代理传值
2016-04-22 16:33
267 查看
/** * 代理传值 如果是多处传值,则代理传值比block方便 */ #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; //初始化导航控制器 UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController: [[RootViewController alloc] init]]; self.window.rootViewController = navi; [self.window makeKeyAndVisible]; return YES; } @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h" #import "LFViewController.h" @interface RootViewController ()<LFViewControllerDelegate>//导入协议 @property(nonatomic, strong) UILabel *city ; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; //添加按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 150, 60); [button setTitle:@"跳到下一个页面" forState:0]; [button setBackgroundColor:[UIColor greenColor]]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; //添加Label self.city = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 60)]; self.city.backgroundColor = [UIColor greenColor]; self.city.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.city]; } /** * 按钮事件 */ - (void)buttonAction:(UIButton*)sender{ /** * 属性传值,从一个控制器push到下一个控制器使用属性传值比较方便 */ LFViewController *lfController = [[LFViewController alloc] init]; lfController.delegate = self;//确定代理 [self.navigationController pushViewController:lfController animated:YES]; } #pragma mark -- LFViewControllerDelegate -- /** * LFViewControllerDelegate协议的方法 * * @param cityName 传递过来的值 */ - (void)passCityName:(NSString *)cityName{ self.city.text = cityName; NSLog(@"cityName: %@",cityName); } @end
#import <UIKit/UIKit.h> @protocol LFViewControllerDelegate;//申明协议 @interface LFViewController : UIViewController /** * 防止循环引用,使用weak */ @property(nonatomic, weak) id<LFViewControllerDelegate> delegate;//设置代理 @end /** * 申请协议 */ @protocol LFViewControllerDelegate <NSObject> /** * LFViewControllerDelegate协议中的方法 * * @param cityName 传递的值 */ - (void)passCityName:(NSString*)cityName; @end
#import "LFViewController.h" @interface LFViewController () @end @implementation LFViewController - (void)viewDidLoad { [super viewDidLoad]; //添加按钮 UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(100, 100, 150, 60); [backBtn setTitle:@"返回且发送通知" forState:0]; [backBtn setBackgroundColor:[UIColor greenColor]]; [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backBtn]; } /** * backBtn按钮的事件 */ - (void)backBtnAction:(UIButton*)sender{ /** * 如果代理能响应对应的方法,则执行该方法 * * @param passCityName: 方法名 */ if ([_delegate respondsToSelector:@selector(passCityName:)]) { [_delegate passCityName:@" London"]; [self.navigationController popViewControllerAnimated:YES]; } } @end
相关文章推荐
- POJ 3220 Jessica's Reading Problem(Two pointers)
- Java 中的堆内存
- JavaScript标准: ES6和ES5
- c#实验7.2:阶乘的异常处理
- Spark使用小结:Java版Join操作
- [转] Objective-C中的@property和@synthesize用法
- UI控件--SwipeRefreshLayout
- SpringMVC——接收请求参数和页面传参
- Tesseract OCR Tutorial
- 经验总结:选课系统和自主招生系统开发总结
- Fedora下用Iptux,中文乱码解决
- Oracle 删除重复数据只留一条
- java集合去重处理
- Oracle表中的一条记录占用weblogic多少内存
- Java 线程池的原理与实现
- 微信电脑版2016 v2.0.0.80 官方正式版
- 被创新工场、君联和华熙集团先后翻牌子,因为它把古风二次元玩得特别溜!
- Hello Blog
- Snackbar源码分析
- c++学习_this指针