您的位置:首页 > 移动开发 > IOS开发

iOS 正向传值demo

2015-10-29 00:00 411 查看
摘要: 第一个界面向第二个界面传值-为正向传值。在第一个界面的文本框输入内容点击传值按钮第二个界面的label标签接收内容

建一个空工程在appdelegate里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

RootViewController *rvc = [[RootViewController alloc] init];
self.window.rootViewController = rvc;
[rvc release];


self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

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

#define kDebugPrint NSLog(@"%s",__func__)
@interface RootViewController ()<UITextFieldDelegate>
{
UITextField *_textField;
}
@end

@implementation RootViewController

/*
正向传值
创建第一个界面   通过第一个界面跳转到第二个界面
如果由第一个界面向第二个界面 进行传值 正向传值
属性传值

第二张向第一张界面传值 反向传值

下级界面向上一级界面传值---》反向传值
*/

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
[self showUI];
}

- (void)showUI {
_textField = [MyControl creatTextFieldWithFrame:CGRectMake(10, 30, 300, 30) placeHolder:@"请输入内容" delegate:self tag:1001];
[self.view addSubview:_textField];

UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 200, 300, 50) target:self sel:@selector(btnClick:) tag:201 image:nil title:@"切换到第二张"];
[self.view addSubview:button];
}

//收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_textField resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)btnClick:(UIButton *)btn {

//
SecondViewController *svc = [[SecondViewController alloc] init];

//正向传值 必须要在 跳转之前传值
//属性传值
svc.textStr =  _textField.text;

//跳转到第二张界面 呈现显示第二张

//second 第一次显示的时候 才调用viewDidLoad在这之前已经把值传过去了
[self presentViewController:svc animated:YES completion:nil];

[svc release];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

//用于正向传值  第一个界面可以通过这个 setter方法 把值传给 第二个界面
@property (nonatomic,copy) NSString *textStr;
@end

#import "SecondViewController.h"
#import "MyControl.h"
#define kDebugPrint NSLog(@"%s",__func__)

@interface SecondViewController ()
{
UILabel *_label;
}
@end

@implementation SecondViewController
- (void)dealloc {
kDebugPrint;

self.textStr = nil;//[_textStr release];
[super dealloc];
}

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self showUI];

}
- (void)showUI {
UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 30, 300, 30) target:self sel:@selector(btnClick:) tag:301 image:nil title:@"返回"];
[self.view addSubview:button];

_label = [MyControl creatLabelWithFrame:CGRectMake(10, 100, 300, 50) text:@""];
_label.backgroundColor = [UIColor grayColor];

//直接赋值  在 创建label的时候已经从第一个界面传过来了
_label.text = self.textStr;

[self.view addSubview:_label];
}
- (void)btnClick:(UIButton *)btn {
//返回上一级
[self dismissViewControllerAnimated:YES completion:nil];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  正向传值