iOS UI07_介面傳值

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.