2013-05-15 57 views
1

我正在研究一個有幾個ViewControllers的應用程序,並且有一個特定的ViewController「login ViewController」,我想從幾乎每個UIViewController都可以訪問它,我知道我可以通過使用segue從每個控制器到LoginViewController,我敢肯定這不是最好的解決方案,那麼實現這一目標的最佳解決方案是什麼?在Storyboard中以編程方式訪問UIViewController

回答

1

你也許可以做這樣的事情

static LoginViewController *instance; //place in the implementation 

- (void) viewDidLoad { 
    instance = self; 
} 

+(LoginViewController *) getInstance { //use this method to access the instance (declare in header) 
    return instance; 
} 

則只需導入你需要訪問它的頭,你就大功告成

7

使用此...變量vc返回你的視圖 - 控制尋找

UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]]; 
    YourViewController *vc = [aStoryboard instantiateViewControllerWithIdentifier:@"YourViewController"]; 
0

我建議你應該創建將由每個需要登錄v控制器被繼承基礎視圖控制器控制器。 然後在你基地的viewController,您可以創建這樣的方法:

-(YourLoginViewController*)giveMeTheLoginController { 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil]; 
    YourLoginViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"YourLoginViewControllerIdentifier"]; 
    return viewController; 
} 

如果你不想,你可以用同樣的方法在您的視圖控制器獲取視圖控制器的新實例基本視圖控制器從故事板。

另外使用每個視圖控制器的segues是一個好方法,segues用於定義您的導航。

1

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllersinYourApplication/UsingViewControllersinYourApplication.html

實例化故事板的視圖控制器編程

列表2-2實例化相同的故事板

- (IBAction)presentSpecialViewController:(id)sender { 
    UIStoryboard *storyboard = self.storyboard; 
    SpecialViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"SpecialViewController"]; 

    // Configure the new view controller here. 

    [self presentViewController:svc animated:YES completion:nil]; 
} 
內的另一個視圖控制器
相關問題