iOS 7 - 在呈现模态视图控制器时收到警告消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18835653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:15:31  来源:igfitidea点击:

iOS 7 - Getting warning message while presenting modal view controller

iosobjective-cuiviewcontroller

提问by Ram G.

Compiling and running using iOS 7 - I am getting warning message: "Presenting view controllers on detached view controllers is discouraged" while presenting modal view controller. I never had problem with iOS 6 or earlier version. Can anyone show if anything changed while presenting the modal view controller?

使用 iOS 7 编译和运行 - 我收到警告消息:“不鼓励在分离的视图控制器上展示视图控制器”,同时展示模态视图控制器。我在使用 iOS 6 或更早版本时从未遇到过问题。任何人都可以显示在呈现模态视图控制器时是否有任何更改吗?

SearchViewController *controller1;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    controller1 = [[SearchViewController alloc] initWithNibName:@"SearchViewController-iPad" bundle:nil];
}
else
{
   controller1 = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
}
controller1.delegate = self;
[[self navigationController] presentModalViewController:controller1 animated:YES];

*EDIT *Here is the code Can someone point out where it is nested. Looks like they are nested, Please suggest how to link using child viewcontroller pattern.

*编辑 *这是代码有人可以指出它的嵌套位置。看起来它们是嵌套的,请建议如何使用子视图控制器模式进行链接。

(void)applicationDidFinishLaunching:(UIApplication *)application
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        self.loginRootviewController =   [[MainViewController alloc] initWithNibName:@"MainViewController-iPad" bundle:nil];
    }
    else
    {
       self.loginRootviewController =   [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    }

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.loginRootviewController];


    DDMenuController *rootController = [[DDMenuController alloc] initWithRootViewController:navController];
    _menuController = rootController;



    AppMainMenuViewController *leftController = [[AppMainMenuViewController alloc] init];
    rootController.leftViewController = leftController;
    self.loginRootviewController.delegateLogin = leftController;

    self.window.rootViewController = rootController;

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

}

采纳答案by Marcus Adams

A view controller is detached if the story board is not aware of any segues that connect that view controller in any way back to the root view controller.

如果故事板不知道以任何方式将该视图控制器连接回根视图控制器的任何 segue,则视图控制器将被分离。

It's suggested that you create a segue via Interface Builder and call it in code, even for a modal view, if you are using a storyboard.

如果您使用故事板,建议您通过 Interface Builder 创建一个 segue 并在代码中调用它,即使对于模态视图也是如此。

Even with XCode 4.6.x and iOS 6.x, you got warnings at build time about unattached view controllers.

即使使用 XCode 4.6.x 和 iOS 6.x,您在构建时也会收到有关未附加视图控制器的警告。

If you have two storyboards (one for iPhone and one for iPad), you can name the segue the same for each. Segue identifiers only have to be unique per storyboard. So, performing a segue (performSegueWithIdentifier) with the same identifier can take you to one place on the iPhone and another on the iPad.

如果您有两个故事板(一个用于 iPhone,一个用于 iPad),您可以为每个故事板命名相同的 segue。每个故事板的 Segue 标识符必须是唯一的。因此,执行具有相同标识符的转场( performSegueWithIdentifier) 可以将您带到 iPhone 上的一个位置和 iPad 上的另一个位置。

回答by Riccardo Tesio

I had the same problem, and me too I was NOT using storyboard (I am working on a three years old project).

我有同样的问题,我也没有使用故事板(我正在做一个三年前的项目)。

In my case the cause was that I did not assign the rootViewController of my application to the window, like this:

就我而言,原因是我没有将应用程序的 rootViewController 分配给窗口,如下所示:

- (BOOL)application:didFinishLaunchingWithOptions:
{
    ...
    self.window.rootViewController = myRootViewController; // I was missing this
    ...
}

No more warnings now.

现在没有更多警告。

回答by Teena nath Paul

This warning normally comes when we try to present a view controller modally in other view controller which is not part of rootViewController, and we just addSubview the view controller's view.

当我们尝试在不属于 rootViewController 的其他视图控制器中以模态方式呈现视图控制器时,通常会出现此警告,并且我们只是 addSubview 视图控制器的视图。

At this stage we should call presentViewController in that view controller which is part of rootViewController.

在这个阶段,我们应该在作为 rootViewController 一部分的视图控制器中调用 presentViewController。

So we can directly present any view controller in rootViewController

所以我们可以直接在 rootViewController 中呈现任何视图控制器

UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[vc presentViewController:obj animated:YES completion:nil];
[vc dismissViewControllerAnimated:YES completion:nil];

回答by bmueller

For those using DDMenuViewController, this is an easy fix. Just add [self addChildViewController:controller];to initWithRootViewController, and to setRightViewControllerand setLeftViewController.

对于那些使用 DDMenuViewController 的人来说,这是一个简单的解决方法。只需添加[self addChildViewController:controller];initWithRootViewController, 和 到setRightViewControllersetLeftViewController

回答by cdescours

To avoid getting the warning in a push navigation, you can directly use :

为了避免在推送导航中收到警告,您可以直接使用:

[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];

And then in your modal view controller, when everything is finished, you can just call :

然后在你的模态视图控制器中,当一切都完成后,你可以调用:

[self dismissViewControllerAnimated:YES completion:nil];

[self dismissViewControllerAnimated:YES completion:nil];

回答by berbie

This message still may appear when you have nested viewcontrollers. If that is the case, make sure they are linked using the child viewcontroller pattern.

当您有嵌套的视图控制器时,此消息仍可能出现。如果是这种情况,请确保使用子视图控制器模式链接它们。

回答by AndyDunn

If you are using storyboard and get this error, it can happen when trying to automatically present a view from viewDidLoad too quickly. If you call your modal segue after a small amount of time it works without any further warnings.

如果您正在使用故事板并收到此错误,则可能会在尝试从 viewDidLoad 过快地自动呈现视图时发生。如果您在一小段时间后调用您的模态 segue,它会在没有任何进一步警告的情况下工作。

回答by oyvindhauge

Calling [self presentViewController:vc animated:NO completion:nil]; in viewDidAppear fixed the issue for me.

调用 [self presentViewController:vc 动画:NO 完成:nil]; 在 viewDidAppear 中为我解决了这个问题。

回答by Dan Levy

I found that, if you are using a storyboard, you will want to put the code that is presenting the new view controller in viewDidAppear. It will also get rid of the "Unbalanced calls to begin/end appearance transitions for..." warning.

我发现,如果您使用的是故事板,您需要将呈现新视图控制器的代码放在 viewDidAppear 中。它还将摆脱“不平衡调用开始/结束外观转换...”警告。

回答by Clive Jefferies

I had the same problem. But rather than it being how I started the view, it was how the view was configured. I had accidentally set the class on the view to the view controller class instead of on the files owner. The clue was that it displayed the view, but some of the styling was missing. The weird thing is that it was working on older versions of iOS.

我有同样的问题。但与其说是我如何启动视图,不如说是视图的配置方式。我不小心将视图上的类设置为视图控制器类而不是文件所有者。线索是它显示了视图,但缺少一些样式。奇怪的是它在旧版本的 iOS 上工作。