button.addTarget(self,action: #selector(buttonAction),forControlEvents: .touchUpInsIDe)
但有没有办法可以做到这样的事情:
button.whenbuttonIsClicked({Insert code here})
这样我甚至没有为按钮声明一个显式函数.我知道我可以使用按钮标签,但我更愿意这样做.
解决方法 创建自己的UIbutton子类来执行此 *** 作:class Mybutton: UIbutton { var action: (() -> VoID)? func whenbuttonIsClicked(action: @escaPing () -> VoID) { self.action = action self.addTarget(self,action: #selector(Mybutton.clicked),for: .touchUpInsIDe) } // button Event Handler: // I have not marked this as @IBAction because it is not intended to // be hooked up to Interface Builder @objc func clicked() { action?() }}
当您以编程方式创建按钮然后调用whenbuttonIsClicked来设置其功能时,将Mybutton替换为UIbutton.
您还可以在故事板中使用UIbuttons(只需将其类更改为Mybutton),然后在vIEwDIDLoad中调用whenbuttonIsClicked.
@IBOutlet weak var thebutton: Mybutton!var count = 0overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // be sure to declare [uNowned self] if you access // propertIEs or methods of the class so that you // don't create a strong reference cycle thebutton.whenbuttonIsClicked { [uNowned self] in self.count += 1 print("count = \(self.count)") }
一个更有能力的实施
认识到程序员可能想要处理比.touchUpInsIDe更多的事件这一事实,我写了这个更强大的版本,它支持每个UIbutton的多个闭包和每个事件类型的多个闭包.
class Closurebutton: UIbutton { private var actions = [UInt : [((UIControl.Event) -> VoID)]]() private let funcDict: [UInt : Selector] = [ UIControl.Event.touchCancel.rawValue: #selector(eventtouchCancel),UIControl.Event.touchDown.rawValue: #selector(eventtouchDown),UIControl.Event.touchDownRepeat.rawValue: #selector(eventtouchDownRepeat),UIControl.Event.touchUpInsIDe.rawValue: #selector(eventtouchUpInsIDe),UIControl.Event.touchUpOutsIDe.rawValue: #selector(eventtouchUpOutsIDe),UIControl.Event.touchdragenter.rawValue: #selector(eventtouchdragenter),UIControl.Event.touchDragExit.rawValue: #selector(eventtouchDragExit),UIControl.Event.touchDragInsIDe.rawValue: #selector(eventtouchDragInsIDe),UIControl.Event.touchDragOutsIDe.rawValue: #selector(eventtouchDragOutsIDe) ] func handle(events: [UIControl.Event],action: @escaPing (UIControl.Event) -> VoID) { for event in events { if var closures = actions[event.rawValue] { closures.append(action) actions[event.rawValue] = closures } else { guard let sel = funcDict[event.rawValue] else { continue } self.addTarget(self,action: sel,for: event) actions[event.rawValue] = [action] } } } private func callActions(for event: UIControl.Event) { guard let actions = actions[event.rawValue] else { return } for action in actions { action(event) } } @objc private func eventtouchCancel() { callActions(for: .touchCancel) } @objc private func eventtouchDown() { callActions(for: .touchDown) } @objc private func eventtouchDownRepeat() { callActions(for: .touchDownRepeat) } @objc private func eventtouchUpInsIDe() { callActions(for: .touchUpInsIDe) } @objc private func eventtouchUpOutsIDe() { callActions(for: .touchUpOutsIDe) } @objc private func eventtouchdragenter() { callActions(for: .touchdragenter) } @objc private func eventtouchDragExit() { callActions(for: .touchDragExit) } @objc private func eventtouchDragInsIDe() { callActions(for: .touchDragInsIDe) } @objc private func eventtouchDragOutsIDe() { callActions(for: .touchDragOutsIDe) }}
演示
class VIEwController: UIVIEwController { var count = 0 overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let button = Closurebutton(frame: CGRect(x: 50,y: 100,wIDth: 60,height: 40)) button.setTitle("press me",for: .normal) button.setTitlecolor(.blue,for: .normal) // Demonstration of handling a single UIControl.Event type. // If your closure accesses self,be sure to declare [uNowned self] // to prevent a strong reference cycle button.handle(events: [.touchUpInsIDe]) { [uNowned self] _ in self.count += 1 print("count = \(self.count)") } // define a second handler for touchUpInsIDe: button.handle(events: [.touchUpInsIDe]) { _ in print("I'll be called on touchUpInsIDe too") } let manyEvents: [UIControl.Event] = [.touchCancel,.touchUpInsIDe,.touchDown,.touchDownRepeat,.touchUpOutsIDe,.touchdragenter,.touchDragExit,.touchDragInsIDe,.touchDragOutsIDe] // Demonstration of handling multiple events button.handle(events: manyEvents) { event in switch event { case .touchCancel: print("touchCancel") case .touchDown: print("touchDown") case .touchDownRepeat: print("touchDownRepeat") case .touchUpInsIDe: print("touchUpInsIDe") case .touchUpOutsIDe: print("touchUpOutsIDe") case .touchdragenter: print("touchdragenter") case .touchDragExit: print("touchDragExit") case .touchDragInsIDe: print("touchDragInsIDe") case .touchDragOutsIDe: print("touchDragOutsIDe") default: break } } self.vIEw.addSubvIEw(button) }}总结
以上是内存溢出为你收集整理的Swift以编程方式为带按钮的按钮创建函数全部内容,希望文章能够帮你解决Swift以编程方式为带按钮的按钮创建函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)