本文共 7588 字,大约阅读时间需要 25 分钟。
第一步:在下一个界面视图控制器的.h文件中定义一个属性 —————————————————————————————— //创建根视图控制器firstVC FirstViewController *firstVC = [[FirstViewController alloc]init]; //创建导航控制器 UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:firstVC]; self.window.rootViewController = navigationController; [firstVC release]; [navigationController release]; ——————————————————————————————— #warning 代理传值第四步 代理对象所在的类遵循协议@interface FirstViewController ()<</span>SecondViewControllerDelegate>@end - (void)passValue:(NSString *)string{ #warning 代理传值第五步 实现协议中的方法 ((UILabel *)[self.view viewWithTag:202]).text = string; - (void)viewWillDisappear:(BOOL)animated{ ((UILabel *)[self.view viewWithTag:202]).text = [SingleLeton__ shareSingleLeton].string; } - (void)viewDidLoad { self.view.backgroundColor = [UIColor cyanColor]; //建立一个label UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 84, 260, 40)]; label.tag = 202; label.backgroundColor = [UIColor whiteColor]; label.text = @"你爱她吗?"; label.textColor = [UIColor redColor]; #warning 单例传值第五步 取出单例中属性中存储的数据,赋值给空间 label.text = [SingleLeton__ shareSingleLeton].string; [self.view addSubview:label]; //建立一个textField UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(30, 164, 260, 40)]; //第三步加tag值 field.tag = 200; field.borderStyle = UITextBorderStyleRoundedRect; field.placeholder = @"请输入内容"; field.textColor = [UIColor redColor]; [self.view addSubview:field]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(30, 244, 260, 40); [button setTitle:@"进入下一页" forState:UIControlStateNormal]; button.tintColor = [UIColor redColor]; button.backgroundColor = [UIColor grayColor]; [button addTarget:self action:@selector(handlePassValue : ) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:button]; [self configureCommonProerty]; self.navigationController.navigationBar.tintColor = [UIColor redColor]; }#pragma mark 公共方法- (void)configureCommonProerty{ //设置导航条背景图片 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"1"] forBarMetrics:UIBarMetricsDefault]; } - (void)handlePassValue : (UIButton *)passValue{ //此时创建的是下一个界面对象 SecondViewController *second = [[SecondViewController alloc]init];#warning 代理传值第三步 为后一个界面指定代理对象,只能是前一个页面试图控制器对象 #warning 属性传值第二步 push之前传入数据 second.testString = ((UITextField *)[self.view viewWithTag:200]).text; [self.navigationController pushViewController:second animated:YES]; [second release]; } - (void)customNavigationItemAppearance{ //配置导航条显示的标题 self.navigationItem.title = @"第一页"; —————————————————————————— #warning 代理传值第一步 定义协议@protocol SecondViewControllerDelegate <</span>NSObject>- (void)passValue : (NSString *)string;@end@interface SecondViewController : UIViewController#warning 属性传值第一步 定义属性且属性的类型要和要传入的数据类型要保持一致 @property(nonatomic,copy)NSString *testString; #warning 代理传值第二步 定义代理属性@property(nonatomic,assign)id<</span>SecondViewControllerDelegate>delegate; ——————————————————————————— //建立一个label UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 84, 260, 40)]; label.backgroundColor = [UIColor whiteColor]; label.text = @"给我下一页的内容";#warning 属性传值的第三步 取出数据让控件显示 label.text = self.testString; label.textColor = [UIColor redColor]; [self.view addSubview:label]; [label release]; //建立一个textField UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(30, 164, 260, 40)]; field.tag = 203; field.borderStyle = UITextBorderStyleRoundedRect; field.placeholder = @"请输入第二页的内容"; field.textColor = [UIColor redColor]; [self.view addSubview:field]; [field release]; //建立一个button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(30, 244, 260, 40); [button setTitle:@"进入下一页" forState:UIControlStateNormal]; button.tintColor = [UIColor redColor]; button.backgroundColor = [UIColor grayColor]; [button addTarget:self action:@selector(handlePassValue :) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:button]; //建立一个button UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(30, 324, 260, 40); [button1 setTitle:@"返回上一页" forState:UIControlStateNormal]; button1.tintColor = [UIColor redColor]; button1.backgroundColor = [UIColor grayColor]; [button1 addTarget:self action:@selector(handlePass :) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:button1]; self.navigationItem.title = @"第二页"; } - (void)handlePassValue : (UIButton *)passValue{ ThirdViewController *third = [[ThirdViewController alloc]init]; //传值第三不创建时赋值 third.textString = ((UITextField *)[self.view viewWithTag:203]).text; [self.navigationController pushViewController:third animated:YES]; [third release]; - (void)handlePass : (UIButton *)pass{ #warning 代理传值第六步 让代理执行协议中的方法 NSString *string = ((UITextField *)[self.view viewWithTag: 203]).text; if ([self.delegate respondsToSelector:@selector(passValue:)]) { [self.delegate passValue:string]; } [self.navigationController popViewControllerAnimated:YES]; —————————————————————————— @property(nonatomic,copy)NSString *textString; self.view.backgroundColor = [UIColor greenColor]; //建立一个label UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 84, 260, 40)]; label.backgroundColor = [UIColor grayColor]; label.text = @"给我第一个页面的内容"; label.text = self.textString; [self.view addSubview:label]; [label release]; //建立一个textField UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(30, 164, 260, 40)]; field.borderStyle = UITextBorderStyleRoundedRect; field.textColor = [UIColor redColor]; field.placeholder = @"啦啦啦"; field.tag = 204; [self.view addSubview:field]; [field release]; //建立一个button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(30, 244, 260, 40); [button setTitle:@"返回" forState:UIControlStateNormal]; button.tintColor = [UIColor redColor]; button.backgroundColor = [UIColor grayColor]; [button addTarget:self action:@selector(handlePassValue :) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:button]; self.navigationItem.title = @"第三页"; } - (void)handlePassValue : (UIButton *)passValue{ #warning 单例传值第四步 给单例对象的属性赋值[SingleLeton__ shareSingleLeton].string = ((UITextField *)[self.view viewWithTag:204]).text; [self.navigationController popToRootViewControllerAnimated:YES]; ———————————————————————————— #warning 单例传值第一步 ,定义单例类,继承自NSObject #import @interface SingleLeton__ : NSObject#warning 单例传值第二步 定义单例类的创建的方法//share stand main 创建单例对象方法常用的开头 + (SingleLeton__ *)shareSingleLeton;#warning 单例传值第三步 定义属性,存储传输的数据,属性的类型要和传输数据的类型保持一致@property(nonatomic,copy)NSString *string; ———————————————————————————— //定义一个有static 修饰的SingleLeton对象//static 修饰的变量的生命周期和应用程序的生命周期一样长,只有程序退出后台的时候才被销毁static SingleLeton__ *single = nil; + (SingleLeton__ *)shareSingleLeton{ //single等于nil还没被初始化,所以在if语句对其初始化 //实时同步单例对象的创建,保护其在多线程下的安全 @synchronized(self){ if (single == nil) { single = [[SingleLeton__ alloc]init]; } } return single; } - (void)dealloc{ self.string = nil; [super dealloc]; ================================================