屬性傳值
顧名思義,屬性傳值是通過(guò)類的屬性來(lái)進(jìn)行值得傳遞.屬性傳值是最容易理解的一種傳值方式.通常程序中頁(yè)面的從前向后傳值應(yīng)用的都是屬性傳值.下面我們來(lái)看一下代碼的實(shí)現(xiàn):
AppDelegate.m
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[vc release];
[nav release];
[_window release];
return YES;
}
ViewController.h
@interface ViewController : UIViewController
@property(nonatomic, copy) NSString *string;
@end
這里聲明的字符串string
就是我們要從第一頁(yè)傳到第二頁(yè)的值.
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@property(nonatomic, retain) UITextField *textField;
@end
@implementation ViewController
- (void)dealloc
{
[_textField release];
[super dealloc];
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"首頁(yè)";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(didClickedRightBarButton:)];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
self.textField.placeholder = @"請(qǐng)輸入文本";
self.textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
}
- (void)didClickedRightBarButton:(UIBarButtonItem *)barButton
{
SecondViewController *vc2 = [[SecondViewController alloc] init];
vc2.secondString = self.textField.text;
//屬性傳值就在此執(zhí)行,在我們push頁(yè)面時(shí)需要在首頁(yè)的按鈕點(diǎn)擊方法中建立一個(gè)新的SecondViewController的對(duì)象,在這里也就是vc2
//而我們?cè)赟econdViewController中已經(jīng)聲明了一個(gè)屬性secondString用來(lái)接收傳過(guò)去的值
//所以vc2現(xiàn)在要進(jìn)行的操作我們可以理解為將要傳的值存在vc2自己的屬性secondString中
[self.navigationController pushViewController:vc2 animated:YES];
}
在ViewController
中僅初始化一個(gè)textField
用來(lái)輸入字符串
直接使用navigationItem
的rightBarButton
創(chuàng)建一個(gè)執(zhí)行push頁(yè)面方法的按鈕,點(diǎn)擊后可以將頁(yè)面推送到第二頁(yè).
SecondViewController.h
@interface SecondViewController : UIViewController
@property(nonatomic, copy) NSString *secondString;
@end
在SecondViewController
中創(chuàng)建一個(gè)屬性secondString
用來(lái)接收第一頁(yè)傳過(guò)來(lái)的值.
@interface SecondViewController ()
@property(nonatomic, retain) UITextField *textField;
@end
@implementation SecondViewController
- (void)dealloc
{
[_textField release];
[super dealloc];
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"第二頁(yè)";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(didClickedLeftBarButton:)];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
self.textField.placeholder = @"請(qǐng)輸入文本";
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.text = self.secondString;
//這里進(jìn)行的就是將之前存在secondString中的值取出來(lái)放在textField中
[self.view addSubview:_textField];
}
- (void)didClickedLeftBarButton:(UIBarButtonItem *)leftButton
{
[self.navigationController popViewControllerAnimated:YES];
}
這里不再自定義button
直接使用系統(tǒng)的按鈕樣式.
首頁(yè)文本框中輸入文本
會(huì)在第二頁(yè)的文本框中進(jìn)行顯示
總結(jié):屬性傳值通常用于從前向后的界面?zhèn)髦?當(dāng)然從后向前通過(guò)屬性傳值也是可以實(shí)現(xiàn)的,不過(guò)不推薦使用.因?yàn)樵诮缑孢^(guò)多的情況下,從后向前的屬性傳值過(guò)于繁瑣(需要通過(guò)下標(biāo)在棧中尋找要傳值的ViewController)且不夠靈活.在之后我寫(xiě)的文章中會(huì)介紹其他幾種傳值方式,更加靈活