我想从uibarbuttonem打开新的视图控制器。
func readQrCode(_ sender:UIBarButtonItem!) {
print("working")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "QRCodeViewController")
}
最佳答案
两种可能性。如果您没有导航控制器(通常是这样),请在右大括号前添加这一行:
self.present(vc, animated:true, completion:nil)
如果您有导航控制器,请改为添加:
self.navigationController?.pushViewController(vc, animated: true)
编辑:(调试导航控制器是否存在)
如果你换了?对一个!当你运行它的时候会出现一个异常,那么导航控制器就不在了。请显示定义导航控制器的代码。我会在AppDelegate中这样做:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let startupVC = SomeViewController()
self.window! = UIWindow(frame: UIScreen.main.bounds)
let mainNavConn = UINavigationController(rootViewController: startupVC)
self.window!.rootViewController = mainNavConn
self.window!.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.5, alpha: 1.0)
self.window!.makeKeyAndVisible()
return true
}
关于ios - 如何获取UIBarButtonItem以打开新的 View Controller Swift 3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42367582/