Controller 和返回 Swift 的按钮操作

标签 button controller navigation swift back

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

相关文章:

android - 是否可以在android按钮中指定边框?

android - 限制Android中按钮的宽度

ruby-on-rails - 如何编写带有身份验证的 Rspec Controller 测试?

android - 在 android 选项卡内导航

jsf - 使用 p :selectOneMenu 导航

android - 带有长文本的对话框按钮不换行/挤压 - android 5.0 lollipop 上的 Material 主题

excel - 在 Excel 中使用 VBA 对运行时添加的命令按钮的引用

c# - 在 Unity3D 中使用第一人称 Controller 在空中移动,c#

java - 如何从 Grails 中的 Controller 访问域属性?

jquery - Sticky.js宽度:100% nav no longer 100% after resize of browser