简体   繁体   English

iOS-笔尖文件中的按钮操作

[英]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. 我想知道将nib文件显示为子视图是否是与隐藏和取消隐藏相同类的视图相比更标准的显示子视图的方式。

Also, 也,

How would i be able to set an action on the buttons in the nib file? 我将如何对nib文件中的按钮进行操作?

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. 即使我的xWork笔尖有一个带有按钮插座的类,但我不确定如何对其进行操作。

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中,调用闭包

@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一个目标动作即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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