码迷,mamicode.com
首页 > 移动开发 > 详细

UI_AppDelegate反向传值

时间:2015-01-02 14:36:34      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

1.新建项目,在AppDelegate.h文件中声明实例变量  

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//添加一个属性
@property (nonatomic,copy)NSString *textString;


@end

2.在ViewController.m文件中定义按钮(button),标签(label)

#import "ViewController.h"
#import "SubViewController.h"
#import "AppDelegate.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   //设置背景颜色
    self.view.backgroundColor = [UIColor orangeColor];
    //创建一个按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    //设置frame
    btn.frame = CGRectMake(50, 50, 100, 50);
    //设置文字
    [btn setTitle:@"切换界面" forState:UIControlStateNormal];
    //增加事件
    [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    //增加到视图中
    [self.view addSubview:btn];
    
    //创建一个UILabel
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 110, 100, 50)];
    //设置背景颜色
    label.backgroundColor = [UIColor redColor];
    //增加到视图中
    [self.view addSubview:label];
    //设置tag值
    label.tag = 100;
}

- (void)clickAction:(id)sender
{
    //创建视图控制器对象(需要导入头文件)
    SubViewController *subCtrl = [[SubViewController alloc] init];
    //切换视图
    [self presentViewController:subCtrl animated:YES completion:nil];
}
//重写viewDidAppear方法
-(void)viewDidAppear:(BOOL)animated
{
    //继承父类,一定要写
    [super viewDidAppear:animated];
    
    //获取应用对象(需要导入头文件)
    UIApplication *app = [UIApplication sharedApplication];
    //获取AppDelegate对象
    AppDelegate *appDelegate = app.delegate;
    //取到UILabel
    UILabel *label = (UILabel *)[self.view viewWithTag:100];
    //修改label上面的文字
    label.text = appDelegate.textString;
}
//在接收到内存警告的时候调用
//释放一些可以恢复的对象
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}

@end

3.新建视图控制器SubViewController

 1 #import "SubViewController.h"
 2 #import "AppDelegate.h"
 3 
 4 @interface SubViewController ()<UITextFieldDelegate>
 5 
 6 @end
 7 
 8 @implementation SubViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view.   
13     self.view.backgroundColor = [UIColor yellowColor]; 
14     //创建一个返回的按钮
15     UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
16     //设置frame
17     btn.frame = CGRectMake(50, 50, 100, 50);
18     //设置文字
19     [btn setTitle:@"返回" forState:UIControlStateNormal];
20     //增加点击事件
21     [btn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
22     //增加到视图上
23     [self.view addSubview:btn];
24     
25     //创建一个UITextField
26     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 110, 100, 50)];
27     //设置borderStyle
28     textField.borderStyle = UITextBorderStyleRoundedRect;
29     //增加到视图上
30     [self.view addSubview:textField];
31     //设置代理,在头文件或者匿名类中增加协议<UITextFieldDelegate>
32     textField.delegate = self;   
33 }
34 - (void)backAction:(id)sender
35 {
36     [self dismissViewControllerAnimated:YES completion:nil];
37 }
38 #pragma mark - UITextFieldDelegate
39 -(BOOL)textFieldShouldReturn:(UITextField *)textField
40 {
41     //收起键盘
42     [textField resignFirstResponder];
43     //取得应用的对象(要导入头文件"AppDelegate.h")
44     UIApplication *app = [UIApplication sharedApplication];
45     //取得AppDelegate类型的对象
46     AppDelegate *appDelegate = app.delegate;
47     //把textField.text这个值传递给appDelegate的textString属性
48     appDelegate.textString =textField.text;
49     
50     return YES;
51 }
52 
53 - (void)didReceiveMemoryWarning {
54     [super didReceiveMemoryWarning];
55     // Dispose of any resources that can be recreated.
56 }
57 
58 @end

 

UI_AppDelegate反向传值

标签:

原文地址:http://www.cnblogs.com/wuyunlong/p/4198392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!