问题描述
限时送ChatGPT账号..实际上我有一个带有两个按钮的自定义视图,我想在运行时通过 UIViewController 隐藏它,所以我没有任何确切的东西可以从 UIViewcontroller 类中隐藏该按钮
Actually i have a Custom view with two button, and i want to hide it at runtime through UIViewController , So i don't get any exact thing to hide that button from UIViewcontroller class
这是我的 CustomView 类,
Here is my CustomView class,
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,然后创建该视图的插座,
I set it to UIView in StoryBoard, and then I create outlet of that view,
@IBOutlet weak var viewBottmNewOrder: BottomButtonUIView!
现在我想从 UIViewcontroller 类中隐藏 btnNewOrder
但是当我使用
Now i want to hide btnNewOrder
from UIViewcontroller class but when i use
viewBottmNewOrder.btnNewOrder.isHidden = true it cause null exception, Please do need full answer.
推荐答案
请不要那样做.required init(coder aDecoder: NSCoder)
当 BottomButtonUIView
从 xib 创建时会调用很多次.您的自定义视图将如下所示:
Please don't do like that. The required init(coder aDecoder: NSCoder)
will call a lot of times when the BottomButtonUIView
created from xib. And your custom view will look like:
[BottomButtonUIView [BottomButtonUIView [btnNewOrder, btnChat]]]
.
因此,当您像这样访问 btnNewOrder
时:viewBottmNewOrder.btnNewOrder
它将为空.
So when you access to btnNewOrder
like that:
viewBottmNewOrder.btnNewOrder
it will null.
我认为您应该在UIViewController"的 viewDidLoad
中添加自定义视图.
I think you should add your custom view in viewDidLoad
of your `UIViewController'.
这篇关于从 UIViewController 类隐藏自定义视图 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论