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

1、iOS传值的方法-属性正向传值

2015-07-17 10:00 363 查看
        用属性进行传值,将A界面的值传递到B界面。在B类中定义一个变量,然后在A类中跳转到B类中的地方,对需要传递的变量进行赋值。

1、A类中的代码如下:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@property (retain, nonatomic) UITextField *textField;

@end

@implementation FirstViewController
@synthesize textField;

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];
self.title = @"属性正向传值";

textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 120, self.view.frame.size.width-20, 40)];
textField.placeholder = @"请输入一个值";
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 35)];
btn.backgroundColor = [UIColor grayColor];
[btn setTitle:@"确定" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}

- (void)btnClick:(id)sender {

SecondViewController *second = [[SecondViewController alloc] init];
second.string = textField.text; //对需要传递的变量进行复制
[self.navigationController pushViewController:second animated:YES];

}

@end


2、B类中中的代码如下

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (retain, nonatomic) NSString *string; //定义传值变量的属性

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize string;

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)];
NSString *str = [NSString stringWithFormat:@"%@:%@",@"你传的值为",string];
label.text = str;
[self.view addSubview:label];
}



      


资源下载地址 : http://download.csdn.net/detail/u010545480/8909409
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS传值