讓我們使用兩個viewControllers的名稱作爲FirstViewController和SecondViewController。
現在假設你從FirstViewController點擊一個按鈕推SecondViewController那麼你需要你的按鈕單擊事件下編寫代碼:
// In the FirstViewController
- (void)buttonClicked:(id)sender{
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle: nil];
second.secString = firstString;
[self.navigationController pushViewController:second animated:YES];
[second release];
}
在這裏,您需要在SecondViewController.h
文件中聲明:
NSString *secString;
然後創建一個@property
@property (non atomic,strong) NSString *secString;
在SecondViewController.m
你需要@synthesize
的secString
:
@synthesize secString;
因此這種方式,您也能夠通過創造財產和合成它創造secString getter和setter。
現在您可以輕鬆訪問secString
,您可以在SecondViewController中的任何位置使用它。
只是爲了檢查嘗試,並檢查是否firstString的值傳遞給secString,寫viewWillAppear:
SecondViewController
的
NSLog(@"secString: %@",secString);
讓我知道如果你需要更多的幫助,下面的代碼。
希望這會有所幫助。
你需要谷歌它 – 2012-03-31 08:23:37
我知道如何傳遞數組,做了很多次,我googling我的情況幾個小時,但我發現沒有工作的解決方案如何傳遞一個NSString值 – 2012-03-31 08:27:08