我需要使用一个按钮来切换页面(即其他视图控制器)。 我有两个控制器(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
}()
我设置了一个函数并使用print语句测试该函数:
@objc func buttonAction(sender: UIButton){
print("button pressed")
}
我执行了这个程序,当我按下按钮时,它会打印出 "button pressed"。
然后我在函数中添加了以下内容:
let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
self.presentViewController(HomeViewController, animated: true, completion: nil)
结果是,它不会切换到其他视图控制器(HomeViewController)。
有什么想法可以让它工作吗?谢谢