gpt4 book ai didi

swift - 从 UIViewController 类隐藏自定义 View UIButton

转载 作者:行者123 更新时间:2023-11-30 12:28:00 24 4
gpt4 key购买 nike

实际上我有一个带有两个按钮的自定义 View ,我想在运行时通过 UIViewController 隐藏它,所以我没有得到任何确切的东西来从 UIViewcontroller 类中隐藏该按钮

这是我的 CustomView 类,

 import UIKit

class BottomButtonUIView: UIView {

@IBOutlet weak var btnNewOrder: UIButton!
@IBOutlet weak var btnChat: UIButton!

override func awakeFromNib() {
super.awakeFromNib()

}

// MARK: init
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
if self.subviews.count == 0 {
setup()
}
}

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

func setup() {
if let view = Bundle.main.loadNibNamed("BottomButtonUIView", owner: self, options: nil)?.first as? BottomButtonUIView {
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

addSubview(view)
}
}


@IBAction func btnOrderNowClick(_ sender: Any) {


let VC1 = StoryBoardModel.orderDeatalStoryBord.instantiateViewController(withIdentifier: "NewOrderViewController") as! NewOrderViewController
VC1.isPush = false
let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.

let currentController = getCurrentVC.getCurrentViewController()
currentController?.present(navController, animated:true, completion: nil)
}
@IBAction func btnChatNowClick(_ sender: Any) {
}

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

}

}

我将其设置为 StoryBoard 中的 UIView,然后创建该 View 的导出,

 @IBOutlet weak var viewBottmNewOrder: BottomButtonUIView!

现在我想从 UIViewcontroller 类中隐藏 btnNewOrder 但当我使用

 viewBottmNewOrder.btnNewOrder.isHidden = true it cause null exception, Please do need full answer.

最佳答案

请不要这样做。当从 xib 创建 BottomButtonUIView 时,必需的 init(coder aDecoder: NSCoder) 将调用很多次。您的自定义 View 将如下所示:

[BottomButtonUIView [BottomButtonUIView [btnNewOrder, btnChat]]]

因此,当您像这样访问 btnNewOrder 时:viewBottmNewOrder.btnNewOrder 它将为空。

我认为你应该在“UIViewController”的 viewDidLoad 中添加自定义 View 。

关于swift - 从 UIViewController 类隐藏自定义 View UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43931617/

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