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;
}
第一步:定义一份协议(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;
}
相关文章推荐
- IOS开发---获取用户当前地理坐标(最新)
- IOS调试技巧——异常断点
- iOS: 如何正确的绘制1像素的线
- 如何让iOS模拟器也能测试蓝牙4.0程序?
- 细数iOS上那些你绝对不会注意到的神细节
- ios-block循环引用简析
- iOS程序运行流程
- iOS自带扫描 和 生成二维码
- iOS应用性能调优的建议和技巧--中高级--王朋
- 如何添加真机调试的iOS设备
- 使用GTMBase64编码解码字符串
- iOS应用性能调优--初级---王朋
- IOS如何以兼容模式运行在iphone4、5、6的屏幕上
- ios属性的扩展
- iOS常用加密方法(aes、md5、base64)
- iOS: 如何正确的绘制1像素的线
- iOS NSData类型
- iOS开发之xib技巧介绍
- iOS常用第三方库大全
- IOS OC 字面量语法