繁体   English   中英

以编程方式显示Storyboard视图

[英]Programmatically displaying Storyboard view

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

顺便说一句,我知道你已经解决了你的问题,但我只想提出另一个问题。 或者,您可以始终从标准的初始场景开始(您可以在应用程序委托中没有任何代码的情况下执行),但让viewWillAppear确定您是否需要条款和条件(T&C),如果需要,则显示单独的场景模态(动画或不动画,如你所见)。 您可以让用户确认T&C然后关闭条款和条件场景,您将回到标准的“初始”场景。

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

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

创造segue

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

segue标识符

现在你的初始场景的viewWillAppear可以执行performSegueWithIdentifier ,例如:

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

    BOOL hasAcceptedTermsAndConditions = ...;

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

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

终于解决了我的问题,我用了这个代码

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

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

希望这有效。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM