gpt4 book ai didi

ios - 以编程方式为所有 UISlider 分配默认操作

转载 作者:可可西里 更新时间:2023-11-01 05:39:59 24 4
gpt4 key购买 nike

我的应用程序处于开发原型(prototype)阶段。有些 slider 没有通过 Storyboard 或编程方式分配给它们任何操作。

我需要在测试期间 slider 拖动停止时显示警告。这可以通过 UISlider 的扩展来完成吗?

当拖动结束以显示警报时,我如何将默认操作分配给 UISlider,除非通过界面生成器或以编程方式分配另一个操作?

Similar Question

最佳答案

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.checkButtonAction()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

}

}

extension UIViewController{
func checkButtonAction(){
for view in self.view.subviews as [UIView] {
if let btn = view as? UISlider {
if (btn.allTargets.isEmpty){
btn.add(for: .allTouchEvents, {
if (!btn.isTracking){
let alert = UIAlertController(title: "Test 3", message:"No selector", preferredStyle: UIAlertControllerStyle.alert)

// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

// show the alert
self.present(alert, animated: true, completion: nil)
}
})
}
}
}

}
}

class ClosureSleeve {
let closure: ()->()

init (_ closure: @escaping ()->()) {
self.closure = closure
}

@objc func invoke () {
closure()
}
}

extension UIControl {
func add (for controlEvents: UIControlEvents, _ closure: @escaping ()->()) {
let sleeve = ClosureSleeve(closure)
addTarget(sleeve, action: #selector(ClosureSleeve.invoke), for: controlEvents)
objc_setAssociatedObject(self, String(format: "[%d]", arc4random()), sleeve, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}

请检查这个修改后的答案。

关于ios - 以编程方式为所有 UISlider 分配默认操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47108942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com