6.1 C
Canberra
Monday, October 27, 2025

ios – BarButtonItem with closure stops working for no cause


The next subject occurs in Xcode 16.2, iOS18 SDK with construct goal iOS17

I’ve an extension on UIBarButtonItem to assist closures as a substitute of goal/motion:


extension UIBarButtonItem {
    typealias Closure = () -> Void

    non-public class UIBarButtonItemClosureWrapper: NSObject {
        let closure: Closure
        init(_ closure: @escaping Closure) {
            self.closure = closure
        }
        
        deinit {
            print("DEINIT")
        }
    }
    
    non-public enum AssociatedKeys {
        static var targetClosure = 1
    }
    
    comfort init(title: String?, fashion: UIBarButtonItem.Type, closure: @escaping Closure) {
        self.init(title: title, fashion: fashion, goal: nil, motion: #selector(closureAction))
        objc_setAssociatedObject(self, &AssociatedKeys.targetClosure, UIBarButtonItemClosureWrapper(closure), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }

    comfort init(picture: UIImage?, fashion: UIBarButtonItem.Type, closure: @escaping Closure) {
        self.init(picture: picture, fashion: fashion, goal: nil, motion: #selector(closureAction))
        objc_setAssociatedObject(self, &AssociatedKeys.targetClosure, UIBarButtonItemClosureWrapper(closure), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }

    @objc
    func closureAction() {
        guard let closureWrapper = objc_getAssociatedObject(self, &AssociatedKeys.targetClosure) as? UIBarButtonItemClosureWrapper else { return }
        closureWrapper.closure()
    }
}

Nonetheless one thing actually unusual is occurring and I do not know why.

I exploit this in a UIViewController in navigationItem.rightBarButtonItems, and that viewcontroller is the foundation viewcontroller of a UINavigationController. All of it works advantageous, till I add a TextField to the SwiftUI View that is contained in that viewcontroller’s UIHostingController little one viewcontroller.

So in abstract:
UINavigationController -> UIViewController -> UIHostingController -> TextField

As quickly as I give the main focus to the TextField, the button not works. You may see it develop into translucent as you faucet it (so it’s registering the faucet), however the closure is not referred to as. If I put a button subsequent to it that makes use of the usual goal/motion system, that one retains on working.

That is the UIViewController code:

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        let merchandise = UIBarButtonItem(title: "TargetAction", fashion: .accomplished, goal: self, motion: #selector(buttonTapped))
        let item2 = UIBarButtonItem(title: "ClosureAction", fashion: .accomplished) {
            print("CLOSURE TEST")
        }
        self.navigationItem.rightBarButtonItems = [item, item2]
    }
    
    @objc func buttonTapped() {
        print("TARGET TEST")
    }

and that is the View code:

struct MyView: View {

    var physique: some View {
        Kind {
            TextField("title", textual content: .fixed("textual content"))
        }
    }

Goal Take a look at is printed each time I faucet on the button. Closure Take a look at is printed after I faucet the opposite button, till I focus the TextField by tapping on it. From then on Goal Take a look at nonetheless works, however Closure Take a look at not prints.

Is the main focus of the TextField and the keyboard look doing one thing with the navigation bar behind the scenes that I’m unaware of, thereby destroying the related objects by some means? Deinit isn’t printed, and placing a breakpoint in ClosureAction reveals the operate is not referred to as both.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles