简单的代表示例?

本文介绍了简单的代表示例?的处理方法,对大家解决问题具有一定的参考价值

问题描述

好的,我正在使用 Objective-C 编程并使用 Xcode.我已经阅读了 Apple 网站上的文档并了解什么是委托,但是当我谈到如何将委托方法实际实现到代码中的部分时,我只是感到困惑,尤其是当他们说现在实现委托的方法."也许只有我一个人,但我不知道在哪里实现该方法(在我只有 ViewController 和 AppDelegate 类的简单情况下,AppDelegate.h/.m 文件是否是正确的位置?).我想我真正学习的最好方法是看一个非常简单的例子.

Ok, I'm programming in objective-C and using Xcode. I have read through the documentation on Apple's website and understand what delegate's are but when I come to the part where it talks about how to actually implement delegate methods into code, I just become confused, especially when they say something like "now implement the delegate's method." Maybe it's just me, but I don't know exactly WHERE to implement the method (would the AppDelegate.h/.m file be the correct location in a simple situation where I only have the ViewController and AppDelegate class?). I guess truly the best way for me to learn is to see a very simple example.

我在下面有一些代码,我想知道是否有人可以通过并告诉我如何将委托连接到 ViewController 以便它显示总和?对不起,如果代码看起来很长,但这是我能想到的最简单的委托示例.为了争论和查看更少的代码(让我更容易看到发生了什么),假设 ServerClass *server 实现了一个服务器,ClientClass *client 实现了一个客户端.两者已经相互连接,正在等待输入他们的号码.我记下了我认为正确的内容,但我确定它不完整(就将委托连接到服务器和客户端而言).我不知道把协议声明放在哪里,所以如果有人可以解决这个简单的问题,它将帮助我了解如何将委托实现到一个类中.

I've got some code below and I was wondering if someone could go through and show me how to connect the delegate to the ViewController so that it shows the sum? Sorry if the code looks long, but this is the simplest delegation example I could think of. For the sake of argument and having less code to look at (making it easier for me to see what's happening), lets say ServerClass *server implements a server and ClientClass *client implements a client. Both are already connected to each other and are waiting to input their number. I put down what I think would be correct but I know for sure it's not complete (as far as connecting the delegate to both server and client). One thing I don't know where to put are the protocol declarations, so if someone could please do this simple problem, it would help me out a lot as far as learning how a delegate is implemented into a class.

顺便说一句,如果有人还想向我展示什么连接到什么,我正在使用 iPhone SDK 3.0 的新 GameKit 中的 Peer Picker.例如,我在 Peer Picker 的 Apple 指南的第 3 步.现在,我不知道第 5 步在我的项目中的位置.感谢所有可以帮助我理解这个委托实现的人......到目前为止,你们都很棒!

By the way, I'm working with the Peer Picker in the new GameKit of the iPhone SDK 3.0 if someone would also like to show me what connects to what. For example, I am at step 3 of the Apple guide for Peer Picker. Now, I don't know where step 5 goes in my project. Thanks to all who can help me understand this delegate implementation...you all have been great so far!

ExampleAppDelegate.h

ExampleAppDelegate.h

#import <UIKit/UIKit.h>

@class ExampleAppViewController;

@interface ExampleAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    ExampleAppViewController *viewController;
    int sum;
}

@property (nonatomic, retain) sum;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ExampleAppViewController *viewController;

-(void) addNum:(int)num;
@end

ExampleAppDelegate.m

ExampleAppDelegate.m

#import "ExampleAppDelegate.h"
#import "ExampleAppViewController.h"

@implementation ExampleAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    application.idleTimerDisabled = YES;

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

-(void)addNum:(int)num {
    sum += num;
}

@end

ExampleAppViewController.h

ExampleAppViewController.h

#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface ExampleAppViewcontroller : NSObject {
        IBOutlet UILabel *sumField; // will display the total sum, one number entered //by the server and one entered by the client, on both iPhones after calculation

        int sum; // the total sum after addition;
        ServerClass *server; // some server
        ClientClass *client; // some client
        int num; // the number to add to sum
}

@property(nonatomic, assign) sum;
@property(nonatomic, retain) num;

-(void) displaySum;
@end

ExampleAppViewController.m

ExampleAppViewController.m

#import "ExampleAppViewcontroller.h"

@implementation ExampleAppViewController

@synthesize sum;
@synthesize num;

-(void) displaySum {
    [sumfield setText: @"%i", sum];
}

@end

推荐答案

我不会对你发布的代码进行任何详细的分析——你能得到的最有用的回应是一些方向,就超越了一般原则的具体的代码示例.以下是一般原则...

I won't go into any detailed analysis of the code you posted — the most helpful response you could get is some direction as far as general principles that transcend a specific code sample. Here are the general principles...

  • 委托是一个对象,其对象(通常)被调用以处理或响应特定事件或操作.
  • 您必须告诉"接受委托的对象您想成为委托.这是通过调用 [object setDelegate:self]; 或设置 object.delegate = self; 在你的代码.
  • 作为委托的对象应该实现指定的委托方法.该对象通常在协议中定义方法,或通过类别在 NSObject 上定义为默认/空方法,或两者​​兼而有之.(正式的协议方法可能更简洁,尤其是现在 Objective-C 2.0 支持可选协议方法.)
  • 当相关事件发生时,调用对象会检查委托是否实现了匹配方法(使用 -respondsToSelector:),如果实现则调用该方法.然后,在将控制权返回给调用者之前,委托人可以控制做任何必须响应的事情.
  • A delegate is an object whose objects are (generally) called to handle or respond to specific events or actions.
  • You must "tell" an object which accepts a delegate that you want to be the delegate. This is done by calling [object setDelegate:self]; or setting object.delegate = self; in your code.
  • The object acting as the delegate should implement the specified delegate methods. The object often defines methods either in a protocol, or on NSObject via a category as default/empty methods, or both. (The formal protocol approach is probably cleaner, especially now that Objective-C 2.0 supports optional protocol methods.)
  • When a relevant event occurs, the calling object checks to see if the delegate implements the matching method (using -respondsToSelector:) and calls that method if it does. The delegate then has control to do whatever it must to respond before returning control to the caller.

在您正在处理的具体示例中,请注意 GKPeerPickerController 有一个名为 delegate 的属性,它接受 id<GKPeerPickerControllerDelegate> 类型的对象.这意味着实现 GKPeerPickerControllerDelegate 协议中的方法的 id(NSObject 的任何子类).GKPeerPickerControllerDelegate 依次定义了许多委托方法并描述了何时他们会被召唤.如果您实现了其中一个或多个方法(文档说所有方法都是可选的,但需要两个)并注册为委托,则将调用这些方法.(请注意,您不需要在 .h 文件中声明方法原型,只需导入协议头并在 .m 文件中实现方法即可.

In the specific example you're working through, notice that GKPeerPickerController has a property named delegate which accepts an object of type id<GKPeerPickerControllerDelegate>. This means an id (any subclass of NSObject) that implements the methods in the GKPeerPickerControllerDelegate protocol. GKPeerPickerControllerDelegate in turn defines a number of delegate methods and describes when they will be called. If you implement one or more of those methods (the documentation says all are optional, but two are expected) and register as a delegate, those methods will be called. (Note that you don't need to declare a method prototype in your .h file, just import the protocol header and implement the method in your .m file.

这篇关于简单的代表示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1158

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1059

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:798

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:884

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:925

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:870

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:854

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:825

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1688

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1294