ios - 尝试在其 View 不在窗口层次结构中的 ViewController 上呈现

标签 ios objective-c iphone

这是在主菜单上使用的代码,当用户按下播放键时,但是当显示该屏幕时 (ViewController.m) 它会显示错误

警告:尝试呈现不在窗口层次结构中的 View !

-(IBAction)play:(id)sender
{
    if(IS_IPAD)
    {
        ViewController *view_obj = [[ViewController alloc]initWithNibName:@"ViewController_iPad" bundle:[NSBundle mainBundle]];
        [self presentViewController:view_obj animated:YES completion:nil];

    }
    else if ([AISGlobal isIphone5thGeneration])
    {
        ViewController *view_obj = [[ViewController alloc]initWithNibName:@"ViewController_iPhone5" bundle:[NSBundle mainBundle]];
        [self presentViewController:view_obj animated:YES completion:nil];


    }
    else
    {
        ViewController *view_obj = [[ViewController alloc]initWithNibName:@"ViewController_iPhone" bundle:[NSBundle mainBundle]];
        [self presentViewController:view_obj animated:YES completion:nil];

    }

}

在 AppDelegate.m 文件中,它在“didFinishLaunchingWithOptions”部分显示了这一点;

if(IS_IPAD)
    {
         self.start_screen_obj = [[Start_screen alloc] initWithNibName:@"Start_screen" bundle:nil];
    }
    else if ([AISGlobal isIphone5thGeneration])
    {
        self.start_screen_obj = [[Start_screen alloc] initWithNibName:@"Start_screen_iphone5" bundle:nil];

    }
    else
    {
       self.start_screen_obj = [[Start_screen alloc] initWithNibName:@"Start_screen_iphone" bundle:nil];
    }

    self.start_screen_obj.view.multipleTouchEnabled = YES;

    self.window.rootViewController = self.start_screen_obj;

    [self.window makeKeyAndVisible];

最佳答案

尝试在 View Controller 的 viewDidAppear 方法中编写您的代码,因为直到那时您的 View 才刚刚创建,并没有添加到 View 层次结构中。

关于ios - 尝试在其 View 不在窗口层次结构中的 ViewController 上呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31139515/

相关文章:

iphone - 当 iPad 日期时间设置更改时通知应用程序

ios - 在@selector 方法中传递参数

iphone - 从背景切换到前景时,UIButton currentimage 不返回图像?

iphone - Twitter+oAuth 引擎在尝试发布状态时返回 401

ios - 未调用 subview 手势识别器

ios - “字体”在 SwiftUI 教程中不可转换为 'Font?'

ios - Firebase segue 身份验证

iphone - iOS 自定义 UIImagePickerController 相机裁剪为正方形

ios - 将一组纹理发送到 Metal 的最佳方式

ios - 将 UIView 保存为 xml