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

前端 未结 2 1603
庸人自扰
庸人自扰 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:28

    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!)
    
    

提交回复
热议问题