detegate委托在IOS中是一种随处可见的模式,通俗的说,就是我把想做的某件事委托给其他人去做,就好像Java中的接口一样,我只用定义方法的实现,不用过问实现的过程。
Demo下载:http://bbs.rosoo.net/thread-14748-1-1.html
- #import <Foundation/Foundation.h>
- @protocol TestDetegate <NSObject>
- - (void)setValue:(NSString *)string;
- @end
在委托中,并没有实现setValue这个方法的内容,而在下面的AView.m中实现这个方法
-
- - (void)setValue:(NSString *)string
- {
- aTextLabel.text = string;
- }
-
- - (IBAction)clickPush:(id)sender
- {
- BView *bview = [[BView alloc] initWithNibName:@"BView" bundle:nil];
- bview.text = @"AView";
-
- bview.detegate = self;
- [self.navigationController pushViewController:bview animated:YES];
- }
在BView.h中
- #import <UIKit/UIKit.h>
- #import "TestDetegate.h"
-
- @interface BView : UIViewController
- @property (nonatomic, retain) IBOutlet UILabel *bTextLabel;
- @property (nonatomic, assign) id <TestDetegate> detegate;
- @property (nonatomic, retain) NSString *text;
- - (IBAction)clickBack:(id)sender;
-
- @end
当点击Push这个Button的时候,设置BView的Label为AView,完成了由父窗口向子窗口传值
当点击Back后返回AView时,通过委托改变AView的label值,实现了子窗口到父窗口的传值
(qq5306546) |