ios 如何在 Swift 4 中以编程方式切换到其他视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48799481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to switch to other view controller programmatically in Swift 4?
提问by J.Kwong
I need to switch page(other view controller) using a button. I have two controllers (viewController to HomeViewController) I have a button created in viewController:
我需要使用按钮切换页面(其他视图控制器)。我有两个控制器(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
}()
and I have set a function and used a print statement to test the function:
我已经设置了一个函数并使用了一个打印语句来测试该函数:
@objc func buttonAction(sender: UIButton){
print("button pressed")
}
I executed the program and when I press the button it prints out "button pressed"
我执行了程序,当我按下按钮时,它会打印出“按下按钮”
Then I added the following into the function:
然后我在函数中添加了以下内容:
let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
self.presentViewController(HomeViewController, animated: true, completion: nil)
As a result, it does not switch to other viewController(HomeViewController).
结果,它不会切换到其他视图控制器(HomeViewController)。
Any idea on how should I make it works? Thank you
关于我应该如何使它工作的任何想法?谢谢
回答by ABS
There are couple of ways to switch to another ViewController.
有几种方法可以切换到另一个 ViewController。
Navigation
Present
导航
展示
If you have created the controllers in storyboard then you have to first get the instance of storyboard in case of multiple storyboards.
如果您在故事板中创建了控制器,那么您必须首先获取故事板的实例,以防有多个故事板。
In case of multiple storyboard you need to mention the name of the storyboard.
如果有多个故事板,您需要提及故事板的名称。
let sampleStoryBoard : UIStoryboard = UIStoryboard(name: "UserBoard", bundle:nil)
Then get the instance of the view controller you wish to switch.
然后获取您要切换的视图控制器的实例。
* Make sure you set the correct Identifier in ViewController.
* 确保您在 ViewController 中设置了正确的标识符。
let homeView = sampleStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
if you wish to present the view controller:
如果你想展示视图控制器:
self.present(homeView, animated: true, completion: nil)
if you wish to push the view controller:
如果你想推送视图控制器:
self.navigationController?.pushViewController(homeView, animated: true)
if you want to Switch to another Controller which you created as Xib
如果你想切换到另一个你创建为 Xib 的控制器
let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
If you want to push:
如果你想推:
self.navigationController?.pushViewController(homeView, animated: true)
Present the Controller:
呈现控制器:
present(homeView, animated: true, completion: nil)
Other ways to Switch between Controllers.
在控制器之间切换的其他方法。
Wiring up the Segues
连接 Segues
Ctrl+Drag from the “View Controller” button, to somewhere in the second View Controller(HomeViewController). It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below.
Ctrl+从“视图控制器”按钮拖动到第二个视图控制器(HomeViewController)中的某处。它可以位于第二个视图控制器的主框中的任何位置。当您释放时,它会显示一个如下所示的框。
in this you don't need any code to switch, it will switch on click of a button.
在这里你不需要任何代码来切换,它会在点击一个按钮时切换。
or you can CTLR + Drag from View controller to other controller to create segue and write below code on click of button action and switch to another view controller.
或者您可以 CTLR + 从视图控制器拖动到其他控制器以创建 segue 并在单击按钮动作时编写以下代码并切换到另一个视图控制器。
performSegue(withIdentifier: "mySegueID", sender: nil)
performSegue(withIdentifier: "mySegueID", sender: nil)
make sure you set the correct identifier of segue.
确保您设置了正确的 segue 标识符。
Details of different segues
不同segue的详细信息
Show — When the View Controllers are in a UINavigationController, this pushes the next View Controller onto the navigation stack. This allows the user to click the back button to return to the previous screen through the back button on the top left of the Navigation Bar.
Show — 当视图控制器位于 UINavigationController 中时,这会将下一个视图控制器推送到导航堆栈上。这允许用户通过导航栏左上角的后退按钮单击后退按钮返回上一屏幕。
Present modally — This presents the next view controller in a modal fashion over the current View Controller. This one doesn't need to be part of a UINavigationController or a UISplitViewController. It just shows the destination View Controller in front of the previous one. This is usually used when the user has to either Finish or Cancel .
模态呈现——这将在当前视图控制器上以模态方式呈现下一个视图控制器。这个不需要是 UINavigationController 或 UISplitViewController 的一部分。它只是在前一个视图控制器前面显示目标视图控制器。这通常在用户必须完成或取消时使用。
Custom — Exactly what it sounds like. You can make your own segue style and transitions and use it here.
自定义 - 正是它听起来的样子。您可以制作自己的转场风格和过渡,并在此处使用它。
回答by Anand Nimje
Switchto anotherviewControllerprogrammatically inSwift 4-
在Swift 4 中以编程方式切换到另一个viewController-
Navigation process
导航过程
first, you need to embed your view with Navigation Controller like -
首先,您需要使用导航控制器嵌入您的视图,例如 -
For Navigation programmatically you can use below code -
对于以编程方式导航,您可以使用以下代码 -
- But this code work with if you working with
StoryBoard
in your project. First, you need to set view identifier in storyBoard
- 但是,如果您
StoryBoard
在项目中使用此代码,则可以使用此代码。首先,您需要在 storyBoard 中设置视图标识符
Push with StoryBoard
Push with StoryBoard
let homeView = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.navigationController?.pushViewController(homeView, animated: true)
Present with StoryBoard
Present with StoryBoard
let homeView = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
present(homeView, animated: true, completion: nil)
Push with XIB
Push with XIB
let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
self.navigationController?.pushViewController(homeView, animated: true)
Present with XIB
Present with XIB
let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
present(homeView, animated: true, completion: nil)
For avoiding any crashes you can also try this code -
为了避免任何崩溃,您还可以尝试使用此代码 -
guard let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil) else {
print("Controller not available")
return
}
self.navigationController?.pushViewController(homeView, animated: true)
回答by iOS Geek
you can switch to a Controller in two ways
您可以通过两种方式切换到控制器
- Present and Dismiss
- Navigation Controller
- 出席和解雇
- 导航控制器
-> First Method is simple
-> 第一种方法很简单
just need to write two lines as :
只需要写两行:
In case Controller in Another storyboard file
如果控制器在另一个故事板文件中
let newStoryBoard : UIStoryboard = UIStoryboard(name: "UserBoard", bundle:nil)
let VC = newStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.present(VC, animated: true, completion: nil)
Or if in same storyboard
或者如果在同一个故事板中
let VC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.present(VC, animated: true, completion: nil)
-> Second using navigation Controller
-> 第二次使用导航控制器
Just make use of a navigation controller and then push or pop controllers in navigation stack
只需使用导航控制器,然后在导航堆栈中推送或弹出控制器
let VC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.navigationController?.pushViewController(VC, animated: true)
In your case You are providing bundle as Nil which is wrong
在您的情况下,您提供的捆绑包为 Nil,这是错误的
let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
///Need to Provide Bundle detail
///需要提供Bundle明细
let HomeViewController = ViewController(nibName: ViewController, bundle: Bundle.main)
回答by Asim Ifitkhar Abbasi
let HomeViewController = ViewController(nibName: "ViewController", bundle: Bundle.main)
self.presentViewController(HomeViewController, animated: true, completion: nil)