iOS 正向传值demo
2015-10-29 00:00
411 查看
摘要: 第一个界面向第二个界面传值-为正向传值。在第一个界面的文本框输入内容点击传值按钮第二个界面的label标签接收内容
建一个空工程在appdelegate里面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
建一个空工程在appdelegate里面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RootViewController *rvc = [[RootViewController alloc] init]; self.window.rootViewController = rvc; [rvc release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import "RootViewController.h" #import "MyControl.h" #import "SecondViewController.h" #define kDebugPrint NSLog(@"%s",__func__) @interface RootViewController ()<UITextFieldDelegate> { UITextField *_textField; } @end @implementation RootViewController /* 正向传值 创建第一个界面 通过第一个界面跳转到第二个界面 如果由第一个界面向第二个界面 进行传值 正向传值 属性传值 第二张向第一张界面传值 反向传值 下级界面向上一级界面传值---》反向传值 */ - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; [self showUI]; } - (void)showUI { _textField = [MyControl creatTextFieldWithFrame:CGRectMake(10, 30, 300, 30) placeHolder:@"请输入内容" delegate:self tag:1001]; [self.view addSubview:_textField]; UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 200, 300, 50) target:self sel:@selector(btnClick:) tag:201 image:nil title:@"切换到第二张"]; [self.view addSubview:button]; } //收键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_textField resignFirstResponder]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } - (void)btnClick:(UIButton *)btn { // SecondViewController *svc = [[SecondViewController alloc] init]; //正向传值 必须要在 跳转之前传值 //属性传值 svc.textStr = _textField.text; //跳转到第二张界面 呈现显示第二张 //second 第一次显示的时候 才调用viewDidLoad在这之前已经把值传过去了 [self presentViewController:svc animated:YES completion:nil]; [svc release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController //用于正向传值 第一个界面可以通过这个 setter方法 把值传给 第二个界面 @property (nonatomic,copy) NSString *textStr; @end
#import "SecondViewController.h" #import "MyControl.h" #define kDebugPrint NSLog(@"%s",__func__) @interface SecondViewController () { UILabel *_label; } @end @implementation SecondViewController - (void)dealloc { kDebugPrint; self.textStr = nil;//[_textStr release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor orangeColor]; [self showUI]; } - (void)showUI { UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 30, 300, 30) target:self sel:@selector(btnClick:) tag:301 image:nil title:@"返回"]; [self.view addSubview:button]; _label = [MyControl creatLabelWithFrame:CGRectMake(10, 100, 300, 50) text:@""]; _label.backgroundColor = [UIColor grayColor]; //直接赋值 在 创建label的时候已经从第一个界面传过来了 _label.text = self.textStr; [self.view addSubview:_label]; } - (void)btnClick:(UIButton *)btn { //返回上一级 [self dismissViewControllerAnimated:YES completion:nil]; } @end
相关文章推荐
- 协议,委托与代理
- iOS中正向、逆向传值的方法总结
- TableViewController for IOS8
- iOS中NSURLSession的使用
- IOS 多线程原理
- iOS本地化存储笔记
- iOS-字典转双模型的实现过程中需要关注的细节
- iOS-字典转双模型的实现过程中需要关注的细节
- iOS获取屏幕的属性
- IOS代理协议与委托
- GitHub上有很多不错的iOS开源项目
- iOS开发之各种动画各种页面切面效果
- ios9有哪些新特性?
- 大神们的微博
- ios学习之xcode编译器的浅谈
- iOS多线程的初步研究-- 锁
- iOS-字典转模型(单模型)的实现
- iOS-字典转模型(单模型)的实现
- iOS之MBProgressHUD用法总结
- 我的iOS学习历程 - OC第三天