代理传值
2016-04-22 16:33 甘雨路 阅读(249) 评论(0) 编辑 收藏 举报/** * 代理传值 如果是多处传值,则代理传值比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