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

对控制器和后退Swift的按钮操作

  •  1
  • TheClassyGuy  · 技术社区  · 10 年前

    我是Swift编程新手,我有一个带有目标的按钮:

    func buttonAction(sender:UIButton!)
    {
        self.window.rootViewController.presentViewController(FirstViewController(), animated: true, completion: nil);
    
    }
    

    然而,我在FirstView(FirstViewController)上有一个按钮,但我想返回MainView(MainViewController),我得到错误代码:

    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!
    

    怎么了?

    1 回复  |  直到 10 年前
        1
  •  0
  •   drewag    10 年前

    最好将代码拆分为单独的View和Controller类。这是很棒的课堂设计。然而,您的控制器应该根据模型视图控制器来处理触摸事件,这是苹果框架使用的范例。视图用于显示数据,控制器用于处理该视图上的用户交互。如果您在控制器上设置操作,那么呈现视图控制器非常简单,因为这是Apple鼓励您分解类的方式:

    func buttonAction(sender:UIButton!)
    {
        self.presentViewController(FirstViewController(), animated: true, completion: nil)
    }