iOS属性传值

在APPDelegate中设置导航

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ViewController *v1 = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:v1];
    
    self.window.rootViewController = nav; 
   return YES;
}

在ViewController中实现lable

#import "ViewController.h"
#import "SecondViewController.h"
#import "Model.h"
@interface ViewController ()
{
    UITextField * textField;
}

@end

@implementation ViewController

- (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];  
}

创建一个继承与NSObject的model;

.H中的
#import 

@interface Model : NSObject
@property(nonatomic,strong)NSString * string;
@end
.M中的

#import "Model.h"

@implementation Model

@end

创建一个控制器

#import 

@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 * laa = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 80, 20)];
    laa.text = @"你的名字";
    [self.view addSubview:laa];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
    NSString *str = [NSString stringWithFormat:@"%@",string];
    label.text = str;
    [self.view addSubview:label];
    
}

你可能感兴趣的:(iOS属性传值)