ios-报错:nil modal view controller

问题描述

报错:nil modal view controller

得到了如下报错:**Application tried to present a nil modal view controller on target**。我在运行的程序是,首先判断条件是否满足,启动后修改初始化视图控制器。

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions
 {
 if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {
    ViewControllerOne *vc1 = [[ViewControllerOne alloc]init];
    vc1=[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];
    [self presentViewController:vc1 animated:YES completion:Nil];
    } else {

    ViewControllerTwo *vc2 = [[ViewControllerTwo alloc]init];
    vc2=[self.storyboard instantiateViewControllerWithIdentifier: @"vc2"];

    [self presentViewController:vc2 animated:YES completion:Nil];
}
// Override point for customization after application launch.
return YES;
}

解决方案

如果是从storyboard中来创建控制器不需要先

ViewControllerOne *vc1 = [[ViewControllerOne alloc]init];

修改后的代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     UIViewController *rootVC;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"logged_in"]) {
           rootVC=[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];
    } else {
           rootVC=[self.storyboard instantiateViewControllerWithIdentifier: @"vc2"];
     }
    window.rootViewConroller=rootVC;
    [self.window makeKeyAndVisible];
    return YES;
}

解决方案二:

我分析你是少了UIWindow:

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    UIViewController *mainViewController = [storyboard instantiateInitialViewController];
    self.window.rootViewController = mainViewController;

    return YES;
}

然后去掉这句:

[storyboard instantiateInitialViewController];

加上这句

[self.storyboard instantiateViewControllerWithIdentifier: @"vc1"];
时间: 2024-08-28 21:50:05

ios-报错:nil modal view controller的相关文章

Modal View Controller Example[转]

In your iPhone app, you'll probably be spending most of the time pushing new view controllers to the stack in order to show screen flow. Sometimes, though, you just want to popup a screen for quick display or input. Here's a quick demo/tutorial on th

Modal View Controller Example – Part 2[转]

n the first part of this tutorial, we set up a pair of simple views in Interface Builder that we switched between modally. In this tutorial, we'll make them somewhat useful and pass data between them using delegates. The concept of protocols and dele

ionic emulate ios/run ios报错问题CDVViewController.h file not found.

xcode 8.0 在终端添加ios平台后,再执行 ionic emulate ios -l -c 结果编译失败,报错说 CDVViewController.h file not found. 然后去到处查试了各种方法,均无果.最后把平台删了重新添加居然就成功了-- 这里总结一下在各地搜寻到的解决这个问题的办法,每种解决办法都能解决一部分人的问题.但具体哪样对自己好使并不一定--以下供有同样问题的同行参考 -------------------------------------- 方法0:重新

【shiro】报错: If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.

spring整合shiro,项目报如下错误: 1 ==============异常开始============= 2 java.lang.IllegalStateException: The mapped controller method class 'com.agen.controller.CorlibController' is not an instance of the actual controller bean instance 'com.sun.proxy.$Proxy45'.

Modal View Controller的不同呈现方式

  ModalViewController可以有不同的呈现方式(modalPresentationStyle),在ipad下要提供多方向支持时,就要注意可能要改变ModalViewController的呈现方式,列举如下: UIModalPresentationFullScreen:全屏模式,即弹出窗口占满整个屏幕,在portrait模式和landscape模式下都一样, UIModalPresentationFormSheet:会将窗口缩小,使之居于屏幕中间,在portrait和landsca

一体机Realtek网卡安装驱动报错

  故障现象: Realtek网卡的一体机安装联想网站上的网卡驱动出现报错"The Realtek Network Controller was not found.If Deep Sleep Mode is enabled Please Plug the Cable." 解决方案: 报错是说明电脑未检测到网卡,但设备管理器中可以看到以太网控制器. 备注: 此方案为临时解决方案,等待联想官网驱动核实更新.

软件开发-安卓,baseadapter,报错,求指教

问题描述 安卓,baseadapter,报错,求指教 logcat如下:开发-安卓,baseadapter,报错,求指教-安卓baseadapter"> content_main: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns

一体机Realtek网卡安装驱动报错的临时解决方案

Realtek网卡的一体机安装联想网站上的网卡驱动出现报错"The Realtek Network Controller was not found.If Deep Sleep Mode is enabled Please Plug the Cable." 解决方案: 报错是说明电脑未检测到网卡,但设备管理器中可以看到以太网控制器. 所以该问题应该是驱动兼容性的问题,

View Controller Transition实现京东加购物车效果

这篇文章中我们主要来叙述一下上述动画效果的实现方案.主要涉及 View Controller 转场动画的知识. 我搭建了个人站点,那里有更多内容,请多多指教.点我哦!!! Presenting a View Controller 显示一个 View Controller 主要有一下几种方式: 使用 segues 自动显示 View Controller: 使用 showViewController:sender: 和 showDetailViewController:sender: 方法显示 V