【问题标题】:How to navigate To a ViewController From xib file of a UIView Class如何从 UIView 类的 xib 文件导航到 ViewController
【发布时间】:2016-02-08 10:11:44
【问题描述】:

我的 xib 文件中有一个包含按钮的视图。当我按下按钮(@IBAction) 时,我想移动到 ViewController。我用过下面的代码

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

我收到错误“'SlideMenuView' 类型的值没有成员'presentViewController'。

因为我的类是 UIView 类型:

class SlideMenuView: UIView { 
}

那么我如何导航到其他视图控制器。

【问题讨论】:

    标签: ios swift uiview xib


    【解决方案1】:

    那是因为您尝试演示的课程是 UIView 而不是 UIViewController。它没有 Present 方法。

    我猜您的视图 (SlideMenuView) 嵌入在视图控制器中。您需要做的是实现一个委托,并通知您包含的 viewController 呈现下一个 Viewcontroller。

    代码如下:

    @protocol SlideMenuViewDelegate: class {
      func slideMenuViewAboutButtonClicked(menuView: SlideMenuView)
    class SlideMenuView: UIView { 
    
    weak var delegate: SlideMenuViewDelegate?
    
    @IBAction func aboutButtonClicked(sender: AnyObject) {
    self.delegate?.slideMenuViewAboutButtonClicked(self)
    }
    

    现在,在您的 viewController 中,实现这个委托方法:

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

    另外,不要忘记将滑块菜单视图对象分配给视图控制器作为委托。 类似:

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

    【讨论】:

    • 感谢您的帮助! @Lirik
    • @S.Bharti,很高兴为您提供帮助。如果您可以点击复选标记,如果您接受了这个答案,我会很高兴。
    【解决方案2】:

    我以不同的方式做到了。在类文件中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-21
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      相关资源
      最近更新 更多