UI07_界面传值

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = naVC;
    [rootVC release];
    [naVC release];


    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
#import "SecondViewController.h"

#pragma mark    第四步: 签订协议
@interface RootViewController ()<SecondViewControllerDelegate>

@property(nonatomic, retain)UITextField *myTextField;
@property(nonatomic, retain)UILabel *backLabel;

@end

@implementation RootViewController
- (void)dealloc
{
    [_myTextField release];
    [_backLabel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //  创建一个textField和label
    self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
    self.myTextField.layer.borderWidth = 1;
    self.myTextField.layer.cornerRadius = 10;
    [self.view addSubview:self.myTextField];
    [_myTextField release];

    self.backLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
    self.backLabel.layer.borderWidth = 1;
    self.backLabel.layer.cornerRadius = 10;
    [self.view addSubview:self.backLabel];
    [_backLabel release];

    //  创建一个下一页的button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 10;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(100, 300, 150, 50);


}

- (void)buttonAction:(UIButton *)button {
    //  点击下一页按钮, 打印textField里的内容
    NSLog(@"%@", self.myTextField.text);

    //  push下一页
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
    //  从前向后进行属性传值

    //  2. 传值
    secondVC.str = self.myTextField.text;

#pragma mark    第五步: 设置代理人
    secondVC.delegate = self;
}
#pragma mark    第六步: 实现协议方法
- (void)takeValue:(NSString *)strValue {
    NSLog(@"%@", strValue);
    //  显示在label上
    self.backLabel.text = strValue;
}

SecondViewController.h

#import <UIKit/UIKit.h>

#pragma mark    协议传值的第一步: 声明一份协议

@protocol SecondViewControllerDelegate <NSObject>

- (void)takeValue:(NSString *)strValue;

@end


@interface SecondViewController : UIViewController

#pragma mark    声明代理人的属性
@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;


//  先写一条属性
@property(nonatomic, copy)NSString *str;

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic, retain)UITextField *textField;


@end

@implementation SecondViewController
- (void)dealloc
{
    [_str release];
    [_textField release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor lightGrayColor];
    NSLog(@"!!!%@", self.str);

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
    [self.view addSubview:self.textField];
    [self.textField release];
    self.textField.layer.borderWidth = 1;
    self.textField.layer.cornerRadius = 10;
    //  接收传过来的值
    self.textField.text = self.str;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.frame = CGRectMake(100, 200, 150, 50);
    [self.view addSubview:button];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];


}

- (void)backAction:(UIButton *)button {
    //  返回上一页
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    [self.navigationController popoverPresentationController];
    [self.navigationController popViewControllerAnimated:YES];

#pragma mark    第三步: 设置代理人执行的协议方法
    [self.delegate takeValue:self.textField.text];

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值