2015-06-02 70 views
-3

我嘗試此代碼,但它對我無效。 謝謝如何在iOS中的不同視圖控制器之間傳遞變量

-(void)passvar:(NSString *)user 
{ 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
UIViewController *viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"signupvc"]; 

viewController.user = user; 

[self presentViewController:viewController animated:YES completion:nil]; 

} 
+0

什麼不正確? – Larme

回答

1

UIViewController不具有user財產。 你應該用你的自定義子類,而不是UIVIewController

-(void)passvar:(NSString *)user 
{ 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
MyCustomViewController *viewController = (MyCustomViewController *)[storyboard instantiateViewControllerWithIdentifier:@"signupvc"]; 

viewController.user = user; 

[self presentViewController:viewController animated:YES completion:nil]; 

} 

注意,在這個代碼,你從故事板實例化一個新的MyCustomViewController,並提交給用戶。它與調用這些代碼行之前打開的任何其他View Controller無關。
如果您調用此函數3次,它將打開3個不同的MyCustomViewController

+0

其工作就像一個魅力....非常感謝你 –

+0

不客氣!請考慮接受答案,如果它幫助:) –

相關問題