如何从viewController的视图中呈现UIViewController

时间:2017-11-10 22:49:49

标签: ios objective-c uiviewcontroller uinavigationcontroller

我使用UITableViewControllerUIViewController的视图,如下所示:

UITableViewController *tvc = [[UITableViewController alloc] init];
[self.view addSubview:tvc.view];

现在,在此UITableViewController内,我希望能够在必要时提供SFSafariViewController。我写了下面的代码:

if (showLink) {
    SFSafariViewController *sfsvc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]];
    [self.presentingViewController.navigationController pushViewController:sfsvc animated:YES];
}

每当我运行它时,我在控制台中收到以下错误,没有任何反应:

[Warning] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<SFSafariViewController: 0x7f8aef82a600>)

我有什么想法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

在这种情况下,使用rootViewController推送SFSafariViewController是一种解决方案。试试下面的代码

if (showLink) {
    SFSafariViewController *sfsvc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]];
    UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window;
    [rootViewController.navigationController pushViewController:sfsvc animated:YES];
}