{"id":557,"date":"2018-02-23T23:40:01","date_gmt":"2018-02-23T15:40:01","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/ios-%e5%a7%94%e6%89%98%ef%bc%88delegates%ef%bc%89\/"},"modified":"2018-02-23T23:40:01","modified_gmt":"2018-02-23T15:40:01","slug":"ios-%e5%a7%94%e6%89%98%ef%bc%88delegates%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=557","title":{"rendered":"iOS \u2013 \u59d4\u6258\uff08Delegates\uff09"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h2>\u59d4\u6258\uff08Delegates\uff09\u793a\u4f8b<\/h2>\n<p>\u5047\u8bbe\u5bf9\u8c61A\u8c03\u7528B\u6765\u6267\u884c\u4e00\u9879\u64cd\u4f5c\uff0c\u64cd\u4f5c\u4e00\u65e6\u5b8c\u6210\uff0c\u5bf9\u8c61A\u5c31\u5fc5\u987b\u77e5\u9053\u5bf9\u8c61B\u5df2\u5b8c\u6210\u4efb\u52a1\u4e14\u5bf9\u8c61A\u5c06\u6267\u884c\u5176\u4ed6\u5fc5\u8981\u64cd\u4f5c\u3002<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u7684\u5173\u952e\u6982\u5ff5\u6709<\/p>\n<ul>\n<li>A\u662fB\u7684\u59d4\u6258\u5bf9\u8c61<\/li>\n<li>B\u5f15\u7528\u4e00\u4e2aA<\/li>\n<li>A\u5c06\u5b9e\u73b0B\u7684\u59d4\u6258\u65b9\u6cd5<\/li>\n<li>B\u901a\u8fc7\u59d4\u6258\u65b9\u6cd5\u901a\u77e5<\/li>\n<\/ul>\n<p>\u521b\u5efa\u4e00\u4e2a\u59d4\u6258\uff08Delegates\uff09\u5bf9\u8c61<\/p>\n<p>1. \u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u89c6\u56fe\u7684\u5e94\u7528\u7a0b\u5e8f<\/p>\n<p>2. \u7136\u540e\u9009\u62e9\u6587\u4ef6 File -&gt; New -&gt; File&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"addNewFile\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/addNewFile.jpg\" width=\"623\" height=\"604\" data-pinit=\"registered\"><\/p>\n<p>3. \u7136\u540e\u9009\u62e9Objective C\u5355\u51fb\u4e0b\u4e00\u6b65<\/p>\n<p>4. \u5c06SampleProtocol\u7684\u5b50\u7c7b\u547d\u540d\u4e3aNSObject\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"setProtocolName\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/setProtocolName.jpg\" width=\"729\" height=\"492\" data-pinit=\"registered\"><\/p>\n<p>5. \u7136\u540e\u9009\u62e9\u521b\u5efa<\/p>\n<p>6.\u5411SampleProtocol.h\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0\u4e00\u79cd\u534f\u8bae\uff0c\u7136\u540e\u66f4\u65b0\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>#import &lt;Foundation\/Foundation.h&gt;\n\/\/ \u534f\u8bae\u5b9a\u4e49\n@protocol SampleProtocolDelegate &lt;NSObject&gt;\n@required\n- (void) processCompleted;\n@end\n\/\/ \u534f\u8bae\u5b9a\u4e49\u7ed3\u675f\n@interface SampleProtocol : NSObject\n\n{\n   \/\/ Delegate to respond back\n   id &lt;SampleProtocolDelegate&gt; _delegate; \n\n}\n@property (nonatomic,strong) id delegate;\n\n-(void)startSampleProcess; \/\/ Instance method\n\n@end<\/pre>\n<p>\n7.\u4fee\u6539 SampleProtocol.m \u6587\u4ef6\u4ee3\u7801\uff0c\u5b9e\u73b0\u5b9e\u4f8b\u65b9\u6cd5\uff1a<\/p>\n<pre>#import \"SampleProtocol.h\"\n\n@implementation SampleProtocol\n\n-(void)startSampleProcess{\n    \n    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate \n\u00a0\u00a0\u00a0\u00a0selector:@selector(processCompleted) userInfo:nil repeats:NO];\n}\n@end<\/pre>\n<p>8. \u5c06\u6807\u7b7e\u4ece\u5bf9\u8c61\u5e93\u62d6\u5230UIView\uff0c\u4ece\u800c\u5728ViewController.xib\u4e2d\u6dfb\u52a0UILabel\uff0c\u5982\u4e0b\u6240\u793a:\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"delegateLabel\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/delegateLabel.jpg\" width=\"1016\" height=\"838\" data-pinit=\"registered\">\n<\/p>\n<p>9. \u521b\u5efa\u4e00\u4e2aIBOutlet\u6807\u7b7e\u5e76\u547d\u540d\u4e3amyLabel\uff0c\u7136\u540e\u6309\u5982\u4e0b\u6240\u793a\u66f4\u65b0\u4ee3\u7801\u5e76\u5728ViewController.h\u91cc\u663e\u793aSampleProtocolDelegate<\/p>\n<pre>#import &lt;UIKit\/UIKit.h&gt;\n#import \"SampleProtocol.h\"\n\n@interface ViewController : UIViewController&lt;SampleProtocolDelegate&gt;\n{\n    IBOutlet UILabel *myLabel;\n}\n@end<\/pre>\n<p>10. \u5b8c\u6210\u6388\u6743\u65b9\u6cd5\uff0c\u4e3aSampleProtocol\u521b\u5efa\u5bf9\u8c61\u548c\u8c03\u7528startSampleProcess\u65b9\u6cd5\u3002\u5982\u4e0b\u6240\u793a\uff0c\u66f4\u65b0ViewController.m\u6587\u4ef6<\/p>\n<pre>#import \"ViewController.h\"\n\n@interface ViewController ()\n\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    SampleProtocol *sampleProtocol = [[SampleProtocol alloc]init];\n    sampleProtocol.delegate = self;\n    [myLabel setText:@\"Processing...\"];\n    [sampleProtocol startSampleProcess];\n\u00a0\u00a0\u00a0\u00a0\/\/ Do any additional setup after loading the view, typically from a nib.\n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n#pragma mark - Sample protocol delegate\n-(void)processCompleted{    \n    [myLabel setText:@\"Process Completed\"];\n}\n\n\n@end<\/pre>\n<p>11. \u5c06\u770b\u5230\u5982\u4e0b\u6240\u793a\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u6700\u521d\u7684\u6807\u7b7e\u4e5f\u4f1a\u7ee7\u7eed\u8fd0\u884c\uff0c\u4e00\u65e6\u6388\u6743\u65b9\u6cd5\u88abSampleProtocol\u5bf9\u8c61\u6240\u8c03\u7528\uff0c\u6807\u7b7e\u8fd0\u884c\u7a0b\u5e8f\u7684\u4ee3\u7801\u4e5f\u4f1a\u66f4\u65b0\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"delegateResult\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/delegateResult.jpg\" width=\"550\" height=\"523\" data-pinit=\"registered\">\n<\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">\u59d4\u6258\uff08Delegates\uff09\u793a\u4f8b \u5047\u8bbe\u5bf9\u8c61A\u8c03\u7528B\u6765\u6267\u884c\u4e00\u9879\u64cd\u4f5c\uff0c\u64cd\u4f5c\u4e00\u65e6\u5b8c\u6210\uff0c\u5bf9\u8c61A\u5c31\u5fc5\u987b&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=557\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[74,93],"class_list":["post-557","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=557"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/557\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}