2012-04-24 34 views
0

我試圖在啓動應用程序時顯示密碼/ pincode(模式視圖控制器)。你可以看到在AppDelegate.h的代碼:啓動應用程序時顯示密碼/ Pincode - Storyboard

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"passcode_in"]) { 
     //display passcode screen 
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
     UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PasscodeViewController"]; 
     [vc setModalPresentationStyle:UIModalPresentationFullScreen]; 

     [self presentModalViewController:vc animated:NO]; 


    } else { 
     NSLog(@"No Passcode Screen"); 
} 

    return YES; 
} 

的問題是,AppDelegate中不支持顯示模態的視圖控制器(presentModalViewController)。我不打算使用.xib文件,只有我的應用程序的Storyboard。有人知道它有什麼問題嗎?任何建議表示讚賞。

議決

我跟着給我以前貼的問題之一指令https://stackoverflow.com/a/10303870/1344459我只添加一些代碼到兩種方法applicationDidEnterBackgroundapplicationWillTerminate在AppDelegate.m爲PinCodeViewController(解決問題模式)啓動應用程序後。現在它運作得如此順利。

回答

0

我對同一個問題的解決方案是建立在另一個故事板視圖控制器,通過自定義SEGUE其鏈接到我的初始視圖控制器,並呼籲在視圖控制器的viewDidLoad方法SEGUE。 LoginSegue.h

#import <UIKit/UIKit.h> 
@interface LoginSegue : UIStoryboardSegue 
@end 

LoginSegue.m

#import "LoginSegue.h" 

@implementation LoginSegue 

- (void)perform { 
    UIViewController *src = (UIViewController *) self.sourceViewController; 
    UIViewController *dst = (UIViewController *) self.destinationViewController; 
    [UIView transitionWithView:src.navigationController.view duration:0.0 
         options:UIViewAnimationTransitionNone 
        animations:^{ 
         [src.navigationController presentViewController:dst animated:NO completion:nil]; 
         } 
        completion:NULL]; 
} 
@end 

然後在故事板,選擇您的賽格瑞並設置SEGUE類LoginSegue和標識符以任何你喜歡的。我是@「登錄」。並在您的viewDidLoad以下幾點:

[self performSegueWithIdentifier:@"toLogin" sender:self]; 
+0

爲了澄清,通過自定義segue呈現此登錄屏幕的ViewController應該是您的初始視圖控制器。 – geraldWilliam 2012-04-24 23:35:44

+0

這對我的其他UIViewControllers來說是一個很好的選擇,但對於Login和PinCode屏幕來說卻不是。在啓動應用程序時,我只在AppDelegate.m中添加了一些代碼到兩個方法** applicationDidEnterBackground **和** applicationWillTerminate **中,用於PinCodeViewController(模態)。現在它運作得如此順利。感謝您的幫助。 – hightech 2012-04-26 14:53:51

0

presentModalViewControllerUIViewController類的方法。你的AppDelegate是一個NSObject或UIResponder,所以無法識別它。

您應該以非模態的方式呈現您的密碼/ pincode視圖,並將其放置在Storyboard的第一個UIViewController中。

如果你需要有模式顯示出來,即使不是必需的,那麼從你的故事板的第一UIViewController中展示你的模式的看法,而不是從AppDelegate中。

在UIViewController你應該寫這樣的事情:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PasscodeViewController"]; 
    [vc setModalPresentationStyle:UIModalPresentationFullScreen]; 

    [self performSelector:@selector(presentModal) 
       withObject:nil 
       afterDelay:0.0]; 
} 

- (void)presentModal { 
    [self presentViewController:vc animated:NO completion:NULL]; 
} 

注:您需要performSelector。如果你不使用它,你的視圖將不會顯示。 請注意,現在不推薦使用presentModalViewController,而應使用presentViewController

+0

我這樣做了,但我只是想出了我需要在AppDelegate.m中爲Pincode模式視圖控制器添加的內容。在啓動應用程序時,我只在AppDelegate.m中添加了一些代碼到兩個方法** applicationDidEnterBackground **和** applicationWillTerminate **中,用於PinCodeViewController(模態)。現在它運作得如此順利。感謝您的幫助。 – hightech 2012-04-26 14:52:17

+0

不客氣! :)請編寫自己的答案,並選擇它來顯示此問題已關閉,並幫助其他程序員解決相同的問題... – Beppe 2012-04-26 16:30:31

0

如果密碼是登錄的先決條件,那麼將其設置爲登錄路徑的一部分可能是有意義的。

爲此在故事板,油漆導航控制器,刪除您在默認情況下得到的UITableViewController根,並設置PasscodeViewController爲根。然後從那裏添加一個push segue到LoginViewController。

在PasscodeViewController的邏輯類似於一直在這裏討論:在viewWillAppear中:它可以檢查密碼要求是否滿足與否。如果需要,請讓密碼視圖出現並執行。如果您已經擁有密碼,請在LoginViewController中執行segue。如果兩者都不需要,請解僱。

最後,一旦PasscodeViewController收集到密碼,它可以決定是否要求登錄(執行推送到LoginViewController),或者只是啓動應用程序(解僱)。

希望有幫助。

+0

我不需要刪除Storyboard中的任何ViewController。我遵循了你在前一篇文章中給出的指示。在啓動應用程序時,我只在AppDelegate.m中添加了一些代碼到兩個方法** applicationDidEnterBackground **和** applicationWillTerminate **中,用於PinCodeViewController(模態)。現在它運作得如此順利。感謝您的幫助。 – hightech 2012-04-26 14:49:31

相關問題