我是 Swift 编程的新手,我有一个带有目标函数的按钮:
func buttonAction(sender:UIButton!)
{
self.window.rootViewController.presentViewController(FirstViewController(), animated: true, completion: nil);
}
但是,我在(FirstViewController 的)FirstView 上有一个按钮,但我想返回到(MainViewController 的)MainView,我得到错误代码:
2014-07-30 00:53:44.545 FifthTry[30275:833440] Warning: Attempt to present
<_TtC8FifthTry19FirstViewController: 0x787b5470> on <_TtC8FifthTry18MainViewController:
0x799802d0> whose view is not in the window hierarchy!
怎么了?
最佳答案
将代码拆分到单独的 View 和 Controller 类中真是太好了。这是很棒的类设计。但是,您的 Controller 应该根据 Model-View-Controller 处理触摸事件,这是 Apple 框架使用的范例。 View 用于显示数据, Controller 用于处理该 View 上的用户交互。如果您在 Controller 上设置操作,则呈现 View Controller 非常容易,因为这是 Apple 鼓励您分解类的方式:
func buttonAction(sender:UIButton!)
{
self.presentViewController(FirstViewController(), animated: true, completion: nil)
}
关于 Controller 和返回 Swift 的按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026282/