代码之家  ›  专栏  ›  技术社区  ›  user4992124

如何从viewController的视图中显示UIViewController

  •  0
  • user4992124  · 技术社区  · 7 年前

    我使用的是 UITableViewController 在一个 UIViewController 如下所示:

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

    现在,在这个里面 表格控制器 我希望能够提出一个 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 回复  |  直到 7 年前
        1
  •  0
  •   trungduc 邱林和    7 年前

    使用 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];
    }