如何从UIView类的xib文件导航到ViewController [英] How to navigate To a ViewController From xib file of a UIView Class
问题描述
我的xib文件中有一个包含按钮的视图。当我按下按钮(@ IBAction)
时,我想移动到ViewController。我使用下面的代码
I have a view in my xib file which contain buttons. i want to move to a ViewController when i will press the button (@IBAction)
. I have used below code
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("About") as! AboutViewController
self.presentViewController(nextViewController, animated: true, completion: nil)
我是得到错误类型'的值'SlideMenuView'没有成员'presentViewController'。
I am getting the error "Value of type 'SlideMenuView' has no member 'presentViewController'.
因为我的类是UIView类型:
because my class is a UIView type :
class SlideMenuView: UIView {
}
所以如何导航到其他视图控制器。
so how can I navigate to other view controller.
推荐答案
我是以不同的方式完成的。在类文件中
I did it in a different way. In class file
类SlideMenuView:UIView {
class SlideMenuView: UIView {
var navigationController:UINavigationController?//声明导航控制器变量
var navigationController: UINavigationController? // Declare a navigation controller variable
//并创建一个带导航控制器的方法
// And create a method which take a navigation controller
func prepareScreen(navController:UINavigationController) - > UIView {
func prepareScreen(navController: UINavigationController)-> UIView {
navigationController = navController
let nibView = NSBundle.mainBundle().loadNibNamed("SlideMenuView", owner: self, options: nil)[0] as! UIView
self.addSubview(nibView)
return nibView
}
//在按钮操作中
@IBAction func btnAction(发件人:UIButton){
@IBAction func btnAction(sender: UIButton) {
var storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyBoard!.instantiateViewControllerWithIdentifier("NextViewController") as! UIViewController
navigationController?.pushViewController(nextViewController, animated: true)
}
}
//从UIViewController调用
// For calling from UIViewController
slideBarMenuIstance.prepareScreen(self.navigationController!)
这篇关于如何从UIView类的xib文件导航到ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!