建一個空工程在appdelegate裏面app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
atom
RootViewController *rvc = [[RootViewController alloc] init]; self.window.rootViewController = rvc; [rvc release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
code
#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