我在我的firstViewController定義爲UITextField如下傳的UITextField文本從一個視圖到另一個
// firstViewController.h
IBOutlet UITextField *PickUpAddress
@property (nonatomic, retain) UITextField *PickUpAddress;
//firstViewController.m
@synthesize PickUpAddress;
// Push secondView when the 'Done' keyboard button is pressed
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if (textField == PickUpAddress) {
SecondViewController *secondViewController= [[SecondViewController alloc]
initWithNibName:@"SecondViewController"
bundle:nil];
secondViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
}
return NO;
}
然後我試圖viewWillAppear中
期間以檢索它在我secondViewController- (void)viewWillAppear:(BOOL)animated {
BookingViewController *bookingViewController = [[BookingViewController alloc] init];
NSString *addressString = [[NSString alloc] init];
addressString = bookingViewController.PickUpAddress.text;
NSLog(@"addressString is %@", bookingViewController.PickUpAddress.text);
}
但它返回在我的控制檯上爲NULL。爲什麼? 在此先感謝:)
您的解決方案確實有效,但爲什麼我們需要使用方法來設置字符串?我們可以只用'@synthesize myString'並設置屬性,'secondViewController.myString = @「stringToSet」;'我可以知道這種方法與你的區別嗎?我看到'釋放'&'保留'被使用,並使我困惑。 –
我認爲我的方法與你的方法 – davartan
@MaTaKazer沒有什麼區別,如果Davartan給你解決方案,請投票。 – azizbekian