ViewA
view 切換前會呼叫 prepareForSegue,
segue 物件同時包含開始及結束兩個 view controllers,
記得幫 Storyboard Segue Identifier
命名 gotoViewB
。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"gotoViewB"]) {
id vc = segue.destinationViewController;
[vc setValue:@"hello world 大家好" forKey:@"dataString"];
}
}
ViewB
在 viewDidLoad 即可取到值。
@property (strong) NSString *dataString;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"output:%@", self.dataString);
//output:hello world 大家好
}