以编程方式显示Storyboard视图

6

我正在使用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;
}

错误

当我没有选择故事板中的初始视图控制器时,我会遇到这个错误。

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

错误

当我选择故事板中的入口点为“Initial View Controller (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'

你的故事板是如何设置的?导航控制器是否是初始控制器?它的根视图控制器是什么? - rdelmar
你可以加入一些错误检查,以查看Storyboard和视图是否成功返回。另外,'(UINavigationController*)self.window.rootViewController' 看起来有点可疑,应该进行测试。 - Jenn
我不明白如何设置Storyboard以及什么是导航控制器的初始控制器?请告诉我如何知道根视图控制器?请解释一下并帮我解决问题。 - Rahul Saini
3个回答

5
顺便说一下,我知道你已经解决了问题,但我想建议另一种方法。你可以选择始终从标准的初始场景开始(在应用程序代理中不需要任何代码),但通过其viewWillAppear确定是否需要使用条款和条件(T&C),如果需要,则模态地呈现该单独场景(根据你的需要进行动画或非动画)。用户确认T&C后,然后关闭条款和条件场景,回到标准的“初始”场景。
如果你这样做,你的T&C将被关闭,你不必在代码中编程更改rootViewController,导航控制器的顶级场景始终保持为标准的“初始”场景,因此像popToRootViewController这样的操作可以轻松完成,你也不必在T&C页面上隐藏导航栏等。它还适用于一种流程,其中你可以向用户提供再次查看T&C的选项(例如,如果你有适当的位置显示它,可以将其放在“设置”或“关于”场景中)。

如果你想知道如何通过编程跳转到T&C页面,你可以从初始场景定义一个segue到T&C场景:

create segue

然后选择该segue并为其指定标识符:

segue identifier

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

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

    BOOL hasAcceptedTermsAndConditions = ...;

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

我很高兴你解决了问题,但我想建议一种替代方案。


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];

0

好的,可能的原因是您在故事板中的控制器中,可能忘记在身份检查器中分配类/控制器名称。接下来,请检查您的plist文件,确保您的故事板条目具有正确的故事板名称。

希望这能解决问题。 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接