比如,第一个界面的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;
}
3914

被折叠的 条评论
为什么被折叠?



