如何在UIView类中调用presentViewController?

Nas*_*han 3 uiview ios presentviewcontroller swift

我的项目中有一个自定义选项卡,位于应用程序的每个屏幕上,因此我创建了自定义UIView类和xib文件,并在其中添加了按钮.现在我希望我的按钮在不同的屏幕上具有不同的story boardID.但我无法presentViewControllerUIView课堂上打电话.以前我在extension课堂上自定义功能,以在屏幕之间导航,但UIView类也无法调用该功能.

import UIKit

@IBDesignable class tab: UIView {

var view: UIView!

@IBAction func btn1(sender: UIButton) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("6")
    self.presentViewController(vc, animated: true, completion: nil)
}
override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
}

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    xibSetup()
}

func xibSetup() {
    view = loadViewFromNib() 
    view.frame = bounds 
    view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
    addSubview(view)
} 
func loadViewFromNib() -> UIView {
    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "tab", bundle: bundle) 
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    return view
} 
}
Run Code Online (Sandbox Code Playgroud)

ak2*_*k2g 7

在您的按钮上调用此功能

func infoClick()  {

        let storyboard: UIStoryboard = UIStoryboard (name: "Main", bundle: nil)
        let vc: CampainDetailView = storyboard.instantiateViewControllerWithIdentifier("campainDetailView") as! CampainDetailView
        let currentController = self.getCurrentViewController()
        currentController?.presentViewController(vc, animated: false, completion: nil)

    }
Run Code Online (Sandbox Code Playgroud)

此函数将创建根视图控制器

func getCurrentViewController() -> UIViewController? {

    if let rootController = UIApplication.sharedApplication().keyWindow?.rootViewController {
        var currentController: UIViewController! = rootController
        while( currentController.presentedViewController != nil ) {
            currentController = currentController.presentedViewController
        }
        return currentController
    }
    return nil

}
Run Code Online (Sandbox Code Playgroud)

上面的代码必须工作,它在Swift 2.1中适用于我


ZHZ*_*ZHZ 5

您可以使用委托。将其添加到 Tab 类中

protocol TabDelegate {

    func didButtonTapped()
}

var delegate: TabDelegate?

@IBAction func btn1(sender: UIButton) {
    delegate?.didButtonTapped()
}
Run Code Online (Sandbox Code Playgroud)

在 viewController 中使用 Tab。像这样设置委托

let tab = Tab()
tab.delegate = self
Run Code Online (Sandbox Code Playgroud)

然后在委托方法中推送新的viewController,并且您可以创建一个BaseViewController来执行此操作,然后它的所有viewController子类都可以具有相同的功能。

class BaseViewController: UIViewController, TabDelegate {

    func didButtonTapped() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("6")
        self.presentViewController(vc, animated: true, completion: nil)
    }
}

Class ViewControllerA: BaseViewController {
}

Class ViewControllerB: BaseViewController {
}
Run Code Online (Sandbox Code Playgroud)