How to navigate To a ViewController From xib file of a UIView Class

南楼画角 提交于 2019-12-04 06:19:37

问题


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)

I am getting the error "Value of type 'SlideMenuView' has no member 'presentViewController'.

because my class is a UIView type :

class SlideMenuView: UIView { 
}

so how can I navigate to other view controller.


回答1:


That is beacuase the class you are trying to present from is a UIView and not a UIViewController. It has no Present method.

I'm guessing your view (SlideMenuView) is embedded inside a viewcontroller. what you need to do is implement a delegate, and inform your containing viewController to present next Viewcontroller.

code below:

@protocol SlideMenuViewDelegate: class {
  func slideMenuViewAboutButtonClicked(menuView: SlideMenuView)
class SlideMenuView: UIView { 

weak var delegate: SlideMenuViewDelegate?

@IBAction func aboutButtonClicked(sender: AnyObject) {
self.delegate?.slideMenuViewAboutButtonClicked(self)
}

now, in your viewController, implement this delegate method:

func slideMenuViewAboutButtonClicked(menuView: SlideMenuView) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("About") as! AboutViewController
self.presentViewController(nextViewController, animated: true, completion: nil)
}

Also, dont forget to assign the sliderMenuView object the viewcontroller as a delegate. something like:

self.sliderMenuView.delegate = self // (self == the containing viewController



回答2:


I did it in a different way. In class file

class SlideMenuView: UIView { 

 var navigationController: UINavigationController? // Declare a navigation controller variable

// And create a method which take a navigation controller 


  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
    }

// In Button action 

 @IBAction func btnAction(sender: UIButton) {

         var storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let nextViewController = storyBoard!.instantiateViewControllerWithIdentifier("NextViewController") as! UIViewController
        navigationController?.pushViewController(nextViewController, animated: true)
    }
}
// For calling from UIViewController

    slideBarMenuIstance.prepareScreen(self.navigationController!)



来源:https://stackoverflow.com/questions/35267012/how-to-navigate-to-a-viewcontroller-from-xib-file-of-a-uiview-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!