天天看點

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 ()<SecondViewControllerDelegate>
@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[email protected]"第一頁";

    self.label=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
    self.label.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:self.label];
    [self.label release];


    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(, , , );
    [button setTitle:@"下一頁" forState:UIControlStateNormal];
    button.layer.borderWidth=;
    button.layer.cornerRadius=;
    [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 <UIKit/UIKit.h>

//協定傳值第一步
//1.聲明一份協定
@protocol SecondViewControllerDelegate <NSObject>

//協定方法
-(void)changeValue:(NSString *)value;

@end

@interface SecondViewController : UIViewController
//2.設定代理人屬性
@property(nonatomic,assign)id<SecondViewControllerDelegate>delegate;




@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[email protected]"第二頁";

    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(, , , );
    [button setTitle:@"傳回" forState:UIControlStateNormal];
    button.layer.borderWidth=;
    button.layer.cornerRadius=;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click:) forControlEvents:
     UIControlEventTouchUpInside];

    self.textfield=[[UITextField alloc] initWithFrame:CGRectMake(, , , )];
    self.textfield.layer.borderWidth=;
    self.textfield.layer.cornerRadius=;
    [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