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=@"第一页";

    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 <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=@"第二页";

    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值