【发布时间】:2018-02-15 03:18:06
【问题描述】:
我需要使用按钮切换页面(其他视图控制器)。 我有两个控制器(viewController 到 HomeViewController) 我在 viewController 中创建了一个按钮:
let acceptButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Accept", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
//button.sendAction(Selector(buttonAction), to: HomeViewController(), for: .touchUpInside)
return button
}()
我已经设置了一个函数并使用了一个打印语句来测试这个函数:
@objc func buttonAction(sender: UIButton){
print("button pressed")
}
我执行了程序,当我按下按钮时,它会打印出“按下按钮”
然后我在函数中添加了以下内容:
let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
self.presentViewController(HomeViewController, animated: true, completion: nil)
结果没有切换到其他viewController(HomeViewController)。
关于我应该如何使它工作的任何想法?谢谢
【问题讨论】:
-
HomeViewController 的 xib 名称是什么?
-
您确定没有在 SO 中找到已在此处询问的类似帖子吗?
-
这是 iOS 非常基本的东西,你应该在发布问题之前在互联网上搜索它(谷歌或 youtube 甚至堆栈溢出)。