我的项目中有一个自定义选项卡,位于应用程序的每个屏幕上,因此我创建了自定义UIView
类和xib
文件,并在其中添加了按钮.现在我希望我的按钮在不同的屏幕上具有不同的story board
ID.但我无法presentViewController
从UIView
课堂上打电话.以前我在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 } }
ak2g.. 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) }
此函数将创建根视图控制器
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 }
上面的代码必须工作,它在Swift 2.1中适用于我
在您的按钮上调用此功能
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) }
此函数将创建根视图控制器
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 }
上面的代码必须工作,它在Swift 2.1中适用于我