简体   繁体   中英

Ios - action on button in nib file

Good evening,

I'm wondering if displaying a nib file as a subview is the more standardized way of displaying a subview when compared to hiding and unhiding a view of the same class.

Also,

How would i be able to set an action on the buttons in the nib file?

let test = xWork.loadViewFromNib()
        test.center = view.center
        self.view.addSubview(test)

Currently doing the above. Even though my xWork nib has a class with an outlet for a button, i'm unsure as to how to set an action to it.

Thank you

Please clarify your first question. As for your second question, how to set the action of a button in a nib, try this:

In your nib, create a callback closure:

var onClickCallback: (Void -> Void)?

and in your button's IBAction, call the closure

@IBAction func buttonAction(sender: IBAction) {
    onClickBallback?()
}

Then, where you use your nib, set the button callback:

let test = xWork.loadViewFromNib()
test.center = view.center
self.view.addSubview(test)
test.onClickCallback = {
    print("Button clicked!")

    // Try this to dismiss the view.
    test.removeFromSuperview()
}

如果您的笔尖文件所有者是其超级视图(或视图控制器),则只需CTRL + DRAG一个目标动作即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM