以编程方式显示Storyboard视图

时间:2013-07-17 14:28:40

标签: iphone ios uistoryboard

我正在使用Storyboard,我正面临错误,如下图所示我的代码已成功执行,但它们没有页面显示或模拟器中的操作仅在启动图像后显示黑屏。

ClsMainPageAppDelegate.h

#import <UIKit/UIKit.h>

@interface ClsMainPageAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

ClsMainPageAppDelegate.m

#import "ClsMainPageAppDelegate.h"
#import "ClsMainPageViewController.h"
#import "ClsTermsandConditionViewController.h"

@implementation ClsMainPageAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.


NSUserDefaults *fetchDefaults = [NSUserDefaults standardUserDefaults];
int message = [fetchDefaults integerForKey:@"checkvalue"];
NSLog(@"Message Hello : %i",message);

if(message == 1)
{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ClsMainPageViewController *mvc = [storyboard instantiateViewControllerWithIdentifier:@"BeIinformedPage"];
    [(UINavigationController*)self.window.rootViewController pushViewController:mvc animated:NO];
    NSLog(@"Launched Home Page");


}
else
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ClsTermsandConditionViewController *mvc = [storyboard instantiateViewControllerWithIdentifier:@"termsandConditionControl"];
    [(UINavigationController*)self.window.rootViewController pushViewController:mvc animated:NO];
    NSLog(@"Launched Terms and Conditions Page");
}
return YES;
}

错误

当我没有在storybroad中选择入口点时,我面临的这个错误是初始视图控制器。

2013-07-17 19:38:12.749 BeInformed[1011:c07] Failed to instantiate the default view
controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry
point is not set?
2013-07-17 19:38:16.127 BeInformed[1011:c07] Message Hello : 0
2013-07-17 19:38:18.333 BeInformed[1011:c07] Launched Terms and Conditions Page

错误

当我在storybroad中选择入口点时,我面临的这个错误是初始视图控制器(termsandConditionControl)

 2013-07-17 19:53:19.839 BeInformed[1057:c07] Message Hello : 0
 2013-07-17 19:53:26.175 BeInformed[1057:c07] - [ClsTermsandConditionViewController
 pushViewController:animated:]: unrecognized selector sent to instance 0x71b2f50
 2013-07-17 19:53:26.176 BeInformed[1057:c07] *** Terminating app due to uncaught 
 exception 'NSInvalidArgumentException', reason: '-[ClsTermsandConditionViewController  
 pushViewController:animated:]: unrecognized selector sent to instance 0x71b2f50'

3 个答案:

答案 0 :(得分:5)

顺便说一下,我知道你已经解决了你的问题,但我只想提出另一个问题。或者,你总是可以从你的标准初始场景开始(你可以在app代理中没有任何代码的情况下开始),但让viewWillAppear确定你是否需要条款和条件(T&amp; C),如果因此,以模态方式呈现单独的场景(动画或不动画,如您所见)。您可以让用户确认T&amp; C然后关闭条款和条件场景,您将回到标准的“初始”场景。

如果您这样做,您的T&amp; C将被解雇,您无需以编程方式更改rootViewController,导航控制器的顶级场景始终保持标准“最初的“场景,所以像popToRootViewController这样的东西没有任何轻微的工作,你不必隐藏T&amp; C页面上的导航栏等。它也适用于你可能呈现用户的流程再次看到T&amp; C的选项(例如,如果你有适当的地方可以显示它,就埋在“设置”或“关于”场景上)。

如果您想知道如何以编程方式转到T&amp; C,您可以定义从初始场景到T&amp; C场景的segue:

create segue

然后选择segue并给它一个标识符:

segue identifier

现在您的初始场景viewWillAppear可以performSegueWithIdentifier,例如:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    BOOL hasAcceptedTermsAndConditions = ...;

    if (!hasAcceptedTermsAndConditions)
    {
        [self performSegueWithIdentifier:@"TermsAndConditions" sender:self];
    }
}

我很高兴你解决了你的问题,但我只想提出一个替代方案。

答案 1 :(得分:4)

最后解决了我的问题,我使用了这个代码

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ClsTermsandConditionViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"termsandConditionControl"];
    UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:ivc];
    self.window.rootViewController=nil;
    self.window.rootViewController = navigationController;
    [navigationController setNavigationBarHidden:YES];
    [self.window makeKeyAndVisible];

答案 2 :(得分:0)

确定可能的原因是,您可能错过了在身份检查器中将类/控制器名称分配给storyBoard中的控制器。接下来还要检查plist文件中是否有故事板条目以及故事板的正确名称。

希望这有效。 :)