2013-02-16 54 views
3

我有一個標籤視圖控制器和與iOS 6.1 SDK中的xcode 4.6登錄視圖控制器IOS 6:呈現模態的視圖控制器

enter image description here

當應用程序開始時加載的「視圖控制器1」 。如果用戶未登錄,如何顯示登錄視圖? 在視圖控制器1的viewDidLoad中()我有插入此代碼:

MyNewAppAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
if(!appDelegate.isUserLogged){ 

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    LoginViewController *controller = (LoginViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginView"]; 
    [self presentViewController:controller animated:YES completion: nil]; 

}

但沒有發生。 如何顯示登錄視圖控制器?

感謝您的支持

+0

確保「控制器」不爲空,並且您已經在StoryBoard中指定了標識符。 – kezi 2013-02-16 15:35:40

+0

「控制器」不爲空,故事板中的ID是正確的! – Tom 2013-02-16 18:18:22

回答

2

我使用Xamarin Studio的4.0和Xcode 4.6和iOS 6.1,我能夠用故事板,顯示登錄屏幕。我的代碼是用C#編寫的,但我相信你可以把它翻譯成Objective-C的等價物。

使用故事板時,窗口和RootViewController已經設置了故事板中的「初始場景」。所以在AppDelegate中,我通過使用它的「Storyboard ID」來實例化我的LoginViewController的一個實例。然後,我緩存當前RootViewController的一個實例,然後將新的LoginViewController設置爲RootViewController。

[Register ("AppDelegate")] 
public partial class AppDelegate : UIApplicationDelegate 
{ 
    LoginViewController loginController; 

    public override UIWindow Window { get; set; } 

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) 
    { 
     loginController = Window.RootViewController.Storyboard.InstantiateViewController("LoginScene") as LoginViewController; 
     loginController.InitialViewController = Window.RootViewController; 

     Window.RootViewController = loginController; 

     return true; 
    } 
    //... other overrides ... 
} 

的LoginViewController裏面我創建了一個屬性以保存InitialViewController和登錄按鈕的動作。在完成登錄工作後,我將RootViewController重置爲已緩存的InitialViewController,然後關閉當前的LoginViewController。

public partial class LoginViewController : UIViewController 
{ 
    public UIViewController InitialViewController { get; set; } 

    public LoginViewController (IntPtr handle) : base (handle) 
    { 
    } 

    partial void OnLoginClicked(MonoTouch.UIKit.UIBarButtonItem sender) 
    { 
     //... do login work here ... 
     UIApplication.SharedApplication.Delegate.Window.RootViewController = InitialViewController; 
     DismissViewController(false, null); 
    } 
} 

LoginViewController可以是獨立的,就像您在故事板中具有的一樣。它不需要連接到任何其他場景或需要任何賽段。

0

這是客觀的C代碼,爲我工作。請注意,在故事板上,我將選項卡欄控制器設置爲根視圖控制器(也就是,「初始視圖控制器」旁邊有一個複選標記)。該代碼覆蓋此設置,以使獨立登錄視圖控制器彈出。

//Note that my storyboard file's name is "Main.storyboard"--here you put the name of the storyboard file WITHOUT The extension, which is why I just say "Main" here.   
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
//On the storyboard, you must set the Storyboard ID of the Login View Controller to the name "LoginForm" that is used below, so the code can find the View Controller referred to 
UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"LoginForm"]; 
self.window.rootViewController = loginController; 

在登錄視圖控制器,當它準備辭退本身,因爲登錄已被驗證是正確的,我呼籲在App代表的方法是這樣的:

//Be sure to import the App Delegate at the top with #import "AppDelegate.h" 
AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
[myAppDelegate showMainScreen]; 

在App代表,這裏是「showMainScreen」方法。請注意,我正在關閉臨時設置爲根視圖控制器的登錄視圖控制器,並將主屏幕放回爲根視圖控制器。

- (void)showMainScreen { 
    [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil]; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    UITabBarController *main = [storyboard instantiateViewControllerWithIdentifier:@"tabBarForm"]; 
    self.window.rootViewController = main; 
} 

一個其他末端:我喜歡每該應用已被最小化作爲一種​​安全措施時間彈出登錄屏幕,所以調用應用程序委託的applicationWillEnterForeground方法的一種方式中登錄控制器交換每次出現時:

- (void)applicationWillEnterForeground:(UIApplication *)application { 
    [self showLoginScreenIfNecessary]; 
} 
相關問題