您将需要获取根视图控制器并从该视图控制器执行 Segue。这对于调试来说可能非常令人沮丧,但是这里有一些关于这个主题的答案。
这是我用来在应用程序收到推送通知时执行从根视图控制器到屏幕的一些代码。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
YourViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewcontrolleridentifier"];
UIViewController *top = [UIApplication sharedApplication].keyWindow.rootViewController;
[top presentViewController:viewController animated:YES completion: nil];
下面是 swift 中的相同代码:
let storyboard = UIStoryboard.init(name: "YourStoryboard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "viewcontrolleridentifier")
let top = UIApplication.shared.keyWindow?.rootViewController
top?.present(viewController, animated: true, completion: nil)
确保在故事板中设置视图控制器标识符。
编辑*如果您正在访问的视图控制器嵌入在导航控制器中,您将需要修改上面的代码,
目标C:
UIViewController *top = [self topMostController];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[top presentViewController:navigationController animated:YES completion: nil];
Swift:
let top = UIApplication.shared.keyWindow?.rootViewController
let navigationController = UINavigationController.init(rootViewController: viewController)
top?.present(navigationController, animated: true, completion: nil)