从 UIViewController 类隐藏自定义视图 UIButton

编程入门 行业动态 更新时间:2024-10-27 08:25:13
本文介绍了从 UIViewController 类隐藏自定义视图 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-24 09:57:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061405.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   视图   UIViewController   UIButton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!