A ----> B 传值
步骤:
1.给B类添加一个属性string
2.A中声明B对象的变量 A中直接调用B属性 例如: B.string = @"TEST";
代码例子:
第一步:
SecondViewController.h
//
// SecondViewController.h
// 属性传值
//
// Created by 萨斯辈的呼唤 on 14-6-13.
// Copyright (c) 2014年 萨斯辈的呼唤. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
// 用于接收外部传给viewController的 值
@property (nonatomic, copy) NSString *string;
@end
SecondViewController.m
//
// SecondViewController.m
// 属性传值
//
// Created by 萨斯辈的呼唤 on 14-6-13.
// Copyright (c) 2014年 萨斯辈的呼唤. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 200, 30)];
label.text = self.string;
[self.view addSubview:label];
[label release];
}
@end
//
// FirstViewController.m
// 属性传值
//
// Created by 萨斯辈的呼唤 on 14-6-13.
// Copyright (c) 2014年 萨斯辈的呼唤. All rights reserved.
//
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 120, 200, 30);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClick:(UIButton *)btn
{
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
secVC.string = @"adf";
[secVC release];
}
@end