您的位置:首页 > 移动开发 > IOS开发

iOS 如何 理解 代理

2015-06-29 10:47 423 查看
比如,第一个界面的label 想通过第二个页面的textfield 修改其值,用代理

第一步:定义一份协议(protocol);

@protocol mydelegate <NSObject>

-(void)changeLabelText;

@end

第二步:第一个界面(viewcontroller)要做代理,必须遵守并实现上面那份协议

#import "mydelegate.h"
@interface ViewController :
UIViewController<mydelegate>

@end

第三步:第二个界面(secondviewcontroller) 定义成员变量 delegate

#import "mydelegate.h"
@interface secondViewController :
UIViewController
{
NSObject<mydelegate> *delegate;
}
@property(nonatomic,retain)NSObject<mydelegate>*delegate;

@end
第四步,设置代理为自己

secondViewController *svc=[[secondViewController
alloc]init];
svc.delegate=self;
[self
presentViewController:svc
animated:YES
completion:nil];
我这里是在button 点击事件里面修改的。跳转下一个页面,设置的代理。
第五步,实现代理方法在第一个页面中

#pragma mark mydelegate
-(void)changeLabelText:str{
UILabel *label=(UILabel *)[self.view
viewWithTag:1001];
label.text=str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: