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

前端 未结 2 1601
庸人自扰
庸人自扰 2021-01-28 20:02

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

<         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-28 20:19

    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
    

提交回复
热议问题