我需要使用按钮切换页面(其他视图控制器)。我有两个控制器(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")
}
我执行了程序,当我按下按钮时,它打印出“按下按钮”
然后我将以下内容添加到函数中:
let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
self.presentViewController(HomeViewController, animated: true, completion: nil)
因此,它不会切换到其他viewController(HomeViewController)。
我应该如何使它有效?谢谢
在Swift 4中以编程方式切换到另一个viewController -
首先,你需要使用导航控制器嵌入你的视图 -
StoryBoard
,则此代码可以使用。首先,您需要在storyBoard中设置视图标识符Push with StoryBoard
let homeView = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.navigationController?.pushViewController(homeView, animated: true)
Present with StoryBoard
let homeView = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
present(homeView, animated: true, completion: nil)
Push with XIB
let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
self.navigationController?.pushViewController(homeView, animated: true)
Present with XIB
let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
present(homeView, animated: true, completion: nil)
为避免任何崩溃,您也可以尝试此代码 -
guard let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil) else {
print("Controller not available")
return
}
self.navigationController?.pushViewController(homeView, animated: true)
有几种方法可以切换到另一个ViewController。
如果您已在故事板中创建了控制器,那么在多个故事板的情况下,您必须先获取故事板的实例。
如果是多个故事板,您需要提及故事板的名称。
let sampleStoryBoard : UIStoryboard = UIStoryboard(name: "UserBoard", bundle:nil)
然后获取要切换的视图控制器的实例。
*确保在ViewController中设置正确的标识符。
let homeView = sampleStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
如果您想呈现视图控制器:
self.present(homeView, animated: true, completion: nil)
如果你想推动视图控制器:
self.navigationController?.pushViewController(homeView, animated: true)
如果要切换到另一个创建为Xib的Controller
let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
如果你想推:
self.navigationController?.pushViewController(homeView, animated: true)
出示控制器:
present(homeView, animated: true, completion: nil)
在控制器之间切换的其他方法。
连接Segues
Ctrl +从“视图控制器”按钮拖动到第二个视图控制器(HomeViewController)中的某个位置。它可以位于第二个视图控制器的主框中的任何位置。当你发布时,它会显示一个类似下面的框。
在这个你不需要任何代码切换,它将打开单击按钮。
或者您可以CTLR +从View控制器拖动到其他控制器以创建segue并在单击按钮操作时写入下面的代码并切换到另一个视图控制器。
performSegue(withIdentifier:“mySegueID”,sender:nil)
确保设置segue的正确标识符。
不同segues的详细信息
显示 - 当视图控制器位于UINavigationController中时,会将下一个视图控制器推送到导航堆栈。这允许用户通过导航栏左上角的后退按钮单击后退按钮返回上一屏幕。
以模态方式呈现 - 这在当前视图控制器上以模态方式呈现下一个视图控制器。这个不需要是UINavigationController或UISplitViewController的一部分。它只是在前一个视图控制器前面显示目标视图控制器。这通常在用户必须完成或取消时使用。
自定义 - 完全是听起来像。您可以制作自己的segue样式和过渡,并在此处使用它。
您可以通过两种方式切换到Controller
- >第一种方法很简单
只需要写两行:
如果控制器在另一个故事板文件中
let newStoryBoard : UIStoryboard = UIStoryboard(name: "UserBoard", bundle:nil)
let VC = newStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.present(VC, animated: true, completion: nil)
或者如果在同一故事板中
let VC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.present(VC, animated: true, completion: nil)
- >二次使用导航控制器
只需使用导航控制器,然后在导航堆栈中按下或弹出控制器即可
let VC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.navigationController?.pushViewController(VC, animated: true)
在你的情况下你提供捆绑为Nil是错误的
let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
///需要提供捆绑详细信息
let HomeViewController = ViewController(nibName: ViewController, bundle: Bundle.main)
let HomeViewController = ViewController(nibName: "ViewController", bundle: Bundle.main)
self.presentViewController(HomeViewController, animated: true, completion: nil)