【问题标题】:Button Action to Controller and Back Swift控制器和返回 Swift 的按钮操作
【发布时间】:2014-07-29 22:56:11
【问题描述】:

我是 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!

怎么了?

【问题讨论】:

  • 这个buttonAction 方法是在 UIViewController 子类上实现的吗?
  • 嗨,我将“视图”从控制器中分离出来,所以我有一个单独的文件用于视图和控制器,在这种情况下,我在视图 Swift-File 中实现了方法

标签: button controller navigation swift back


【解决方案1】:

将代码拆分为单独的 View 和 Controller 类非常棒。这是很棒的班级设计。但是,您的控制器应该根据 Model-View-Controller 处理触摸事件,这是 Apple 框架使用的范例。视图用于显示数据,控制器用于处理该视图上的用户交互。如果您在控制器上设置操作,则显示视图控制器非常容易,因为这是 Apple 鼓励您分解类的方式:

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

【讨论】:

  • 非常感谢!这种拆分非常好,但是如果您想做一些小事情,比如导航到另一个视图,很多事情都需要背景知识。所以这解决了它,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多