Ios 如何在Swift 4中以编程方式切换到其他视图控制器?
我需要使用按钮切换页面(其他视图控制器)。 我有两个控制器(viewController到HomeViewController) 我在viewController中创建了一个按钮:Ios 如何在Swift 4中以编程方式切换到其他视图控制器?,ios,swift,Ios,Swift,我需要使用按钮切换页面(其他视图控制器)。 我有两个控制器(viewController到HomeViewController) 我在viewController中创建了一个按钮: let acceptButton: UIButton = { let button = UIButton(type: .system) button.setTitle("Accept", for: .normal) button.setTitleColor(.blue, for: .norma
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)
我执行了程序,当我按下按钮时,它会打印出“按钮按下”
然后,我在函数中添加了以下内容:
@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-
let HomeViewController = ViewController(nibName: "ViewController", bundle: Bundle.main)
self.presentViewController(HomeViewController, animated: true, completion: nil)
导航过程
首先,您需要使用导航控制器嵌入视图,如-
对于以编程方式进行导航,可以使用以下代码-
- 但是,如果您在项目中使用
,则此代码可以使用。 首先,您需要在情节提要中设置视图标识符情节提要
使用故事板推送
与情节提要一起呈现
使用XIB推送
与XIB一起出现
为了避免任何崩溃,您也可以尝试以下代码-
您可以通过两种方式切换到控制器
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)
在您的情况下,您提供的捆绑包为零,这是错误的
let HomeViewController = ViewController(nibName: ViewController, bundle: nil)
///需要提供捆绑详细信息
let HomeViewController = ViewController(nibName: ViewController, bundle: Bundle.main)
有两种方法可以切换到另一个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)
self.navigationController?.pushViewController(homeView, animated: true)
present(homeView, animated: true, completion: nil)
如果要按视图控制器:
self.present(homeView, animated: true, completion: nil)
self.navigationController?.pushViewController(homeView, animated: true)
self.navigationController?.pushViewController(homeView, animated: true)
present(homeView, animated: true, completion: nil)
如果要切换到另一个创建为Xib的控制器
let homeView = HomeViewController(nibName: "HomeViewController", bundle: nil)
如果要推送:
self.present(homeView, animated: true, completion: nil)
self.navigationController?.pushViewController(homeView, animated: true)
self.navigationController?.pushViewController(homeView, animated: true)
present(homeView, animated: true, completion: nil)
呈现控制器:
self.present(homeView, animated: true, completion: nil)
self.navigationController?.pushViewController(homeView, animated: true)
self.navigationController?.pushViewController(homeView, animated: true)
present(homeView, animated: true, completion: nil)
在控制器之间切换的其他方式。
let homeView = sampleStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
连接各个分段
按住Ctrl键并从“视图控制器”按钮拖动到第二个视图控制器(HomeViewController)中的某个位置。它可以位于第二个视图控制器主框中的任何位置。当您释放时,它将显示一个类似下面的框
在这种情况下,您不需要任何代码来切换,只需单击按钮即可切换
或者,您可以CTLR+从视图控制器拖动到另一个控制器,在单击按钮操作并切换到另一个视图控制器时创建segue并编写以下代码
performsgue(标识符为:“mySegueID”,发件人:nil)
确保设置了正确的segue标识符。
let homeView = sampleStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
不同阶段的详细信息
显示-当视图控制器位于UINavigationController中时,这会将下一个视图控制器推送到导航堆栈上。这允许用户单击后退按钮,通过导航栏左上角的后退按钮返回上一屏幕
以模态方式呈现-以模态方式呈现当前视图控制器上的下一个视图控制器。这个不需要是UINavigationController或UISplitViewController的一部分。它只是在前一个视图控制器前面显示目标视图控制器。这通常在用户必须完成或取消时使用
定制-听起来就是这样。您可以创建自己的segue样式和转换,并在此处使用。HomeViewController的xib名称是什么?您确定没有在SO中找到类似的帖子吗?这是iOS的基本功能,您应该在internet上搜索它(google或youtube,甚至stack overflow)在发布问题之前。我将您的建议添加到函数中,它给了我一个错误:libc++abi.dylib:以NSException类型的未捕获异常终止。我检查了我没有任何已断开的触发分段或任何已断开的出口。有什么想法吗?非常感谢。