iOS UI07_介面傳值
//// MainViewController.m// UI07_介面傳值//// Created by dllo on 15/8/6.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import MainViewController.h#import SecondViewController.h//4.簽訂協議@interface MainViewController ()@property(nonatomic,retain)UILabel *label;@property(nonatomic,retain)UIButton *button;@end@implementation MainViewController-(void)dealloc{ [_label release]; [_button release]; [super dealloc];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor orangeColor]; //設定成不透明 self.navigationController.navigationBar.translucent=NO; self.title=@第一頁; self.label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; self.label.backgroundColor=[UIColor yellowColor]; [self.view addSubview:self.label]; [self.label release]; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(100, 200, 100, 40); [button setTitle:@下一頁 forState:UIControlStateNormal]; button.layer.borderWidth=1; button.layer.cornerRadius=10; [self.view addSubview:button]; [button addTarget:self action:@selector(click:) forControlEvents: UIControlEventTouchUpInside];}-(void)click:(UIButton *)button{ SecondViewController *secVC=[[SecondViewController alloc] init]; [self.navigationController pushViewController:secVC animated:YES ]; [secVC release]; //5.設定代理人 secVC.delegate=self;}//6.實現協議方法-(void)changeValue:(NSString *)value{ NSLog(@%@,value); self.label.text=value;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
//// SecondViewController.h// UI07_介面傳值//// Created by dllo on 15/8/6.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import //協議傳值第一步//1.聲明一份協議@protocol SecondViewControllerDelegate //協議方法-(void)changeValue:(NSString *)value;@end@interface SecondViewController : UIViewController//2.設定代理人屬性@property(nonatomic,assign)iddelegate;@end
//// SecondViewController.m// UI07_介面傳值//// Created by dllo on 15/8/6.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import SecondViewController.h@interface SecondViewController () //一個textfield 一個button@property(nonatomic,retain)UITextField *textfield;@property(nonatomic,retain)UIButton *button;@end@implementation SecondViewController-(void)dealloc{ [_button release]; [_textfield release]; [super dealloc];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor =[UIColor cyanColor]; self.title=@第二頁; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(100, 200, 100, 40); [button setTitle:@返回 forState:UIControlStateNormal]; button.layer.borderWidth=1; button.layer.cornerRadius=10; [self.view addSubview:button]; [button addTarget:self action:@selector(click:) forControlEvents: UIControlEventTouchUpInside]; self.textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; self.textfield.layer.borderWidth=1; self.textfield.layer.cornerRadius=10; [self.view addSubview:self.textfield]; [self.textfield release];}//協議的觸發條件是點擊按鈕,所以在這裡進行協議傳值第三步//3.設定代理人執行協議的方法-(void)click:(UIButton *)button{ [self.navigationController popToRootViewControllerAnimated:YES]; [self.delegate changeValue:self.textfield.text];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end