2015-15-16- UI8_协议传值

//
// AppDelegate.h
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder

@property (retain, nonatomic) UIWindow *window;

@end


//
// AppDelegate.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

#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];
    RootViewController *rootVC = [[RootViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rootVC];
    self.window.rootViewController = navi;
    [navi release];
    [rootVC release];

return YES;

}

  • (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end



//
// RootViewController.h
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


//
// RootViewController.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

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

#warning 4.签订协议
@interface RootViewController ()

@property (nonatomic, retain)UITextField *textField;
@end

@implementation RootViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    // tetxField
    // 通过属性创建对象的两种写法
    // 1.只在releas处使用_textField
    // 2.只在创建的时候使用self.textField =
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    _textField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_textField];
    [_textField release];

// button

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];

[button addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];
[button release];

self.title = @"首页";

}

  • (void)go
    {
    SecondViewController *secVC = [[SecondViewController alloc]init];
    // 2.属性赋值
    // 想怎么就怎么赋值
    secVC.str = _textField.text;
    //只能赋值给title
    // secVC.title = _textField.text;
    #warning 5.设置代理人(创建对象之后 push页面之前)
    secVC.delegate = self;
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
    }
    #warning 6.实现协议方法
    // 参数中为 第二页输入框的texz字符串

  • (void)passValue:(NSString *)string
    {
    _textField.text = string;
    }

  • (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
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

#warning 界面通信之协议传值(2->1)
// 2.页面上的值想要改变1页面上的控件 无法直修改 设置1页面对象为代理人 帮助修改
#warning 1.声明协议

@protocol PassDelegate

  • (void)passValue:(NSString *)string;

@end

@interface SecondViewController : UIViewController
// 1.在第二页定义保存数据的属性
@property (nonatomic, copy)NSString *str;

#warning 2.设置代理人属性
@property (nonatomic, assign)id delegate;
@end


//
// SecondViewController.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@property (nonatomic, retain)UITextField *textField;
@end

@implementation SecondViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
// 3.使用传递过来的值
self.title = self.str;


// textField
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
_textField.backgroundColor = [UIColor yellowColor];
// 第一页传值到第二页textfield
_textField.text = self.str;
[self.view addSubview:_textField];
[_textField release];

// button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
button.backgroundColor = [UIColor redColor];

[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

// 遍历器不能release
[button release];

// 僵尸模式 Zombie Object
// 过度释放时使用 查找对应的被过度释放的地方

// 第三页
UIButton *buttonT = [[UIButton alloc]initWithFrame:CGRectMake(100, 250, 100, 30)];
buttonT.backgroundColor  = [UIColor redColor];
[buttonT addTarget:self action:@selector(buttonT) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonT];
[buttonT release];

}
// 第三页

  • (void)buttonT
    {
    ThirdViewController *thVC = [[ThirdViewController alloc ]init];
    //
    thVC.thc = self.textField.text;

    //
    thVC.delegate = self;
    [self.navigationController pushViewController:thVC animated:YES];

    [thVC release];
    }

  • (void)pass:(NSString *)string
    {
    _textField.text = string;
    }

  • (void)back
    {
    #warning 3.代理人调用协议方法(页面前返回)
    [self.delegate passValue:_textField.text];

    //

[self.navigationController popToRootViewControllerAnimated:YES];

}

  • (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



//
// ThirdViewController.h
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol pp

  • (void)pass:(NSString *)string;

@end
@interface ThirdViewController : UIViewController
@property (nonatomic, copy)NSString *thc;
@property (nonatomic, assign) id delegate;
@end


//
// ThirdViewController.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()
@property (nonatomic, retain)UITextField *textField;
@end

@implementation ThirdViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.title = self.thc;

// self.textField.text = self.thc;
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
_textField.backgroundColor = [UIColor yellowColor];
// [_textField addTarget:self action:@selector(aa) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_textField];
[_textField release];
_textField.text = self.thc;

//


UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
button.backgroundColor = [UIColor redColor];

[self.view addSubview:button];

[button addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
[button release];

}

  • (void)button

{
//
[self.delegate pass:_textField.text];

//
[self.navigationController popViewControllerAnimated:YES];

}

  • (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

标签: ios, ios开发, ui

添加新评论

Title - Artist
0:00