gpt4 book ai didi

ios - 如何在 UIView 类中调用 presentViewController?

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:24 24 4
gpt4 key购买 nike

我的项目中有一个自定义选项卡,它位于应用程序的每个屏幕上,因此我创建了自定义 UIView 类和 xib 文件,并在其中添加了按钮。现在我想要我的按钮在具有不同 story board id 的不同屏幕上进行导航。但是我不能从 UIView 类中调用 presentViewController。以前我在 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
}
}

最佳答案

在你的按钮上调用这个函数

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)

}

这个函数将创建 Root View Controller

 func getCurrentViewController() -> UIViewController? {

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

}

上面的代码必须有效,它适用于 Swift 4.0

关于ios - 如何在 UIView 类中调用 presentViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34649173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com