【问题标题】:UIButton not updating state (Selected/Default) dynamically when disconnected from Xcode与 Xcode 断开连接时,UIButton 不会动态更新状态(选定/默认)
【发布时间】:2021-07-30 00:28:35
【问题描述】:

我创建了一个很好用的 UIButton 集,但仅在连接到 Xcode 时(在运行的模拟器和连接的 iPhone 中)。

如果我正在运行模拟器,则选择的按钮将是唯一选择的按钮

如果我“停止”模拟器,任何被选中的按钮都将保持选中的 UI 状态,即使代码专门将其标记为取消选中。

我不确定是什么导致了这种变化,以及为什么在连接模拟器时它会起作用(我可以看到日志,很不方便)。

这是我用来更新按钮状态的代码(我已使用情节提要中的“按钮背景图像”连接图像

        if format == "A" {
            button1.isSelected = true
            button2.isSelected = false
            button3.isSelected = false
        } else if format == "B" {
            button1.isSelected = false
            button2.isSelected = true
            button3.isSelected = false
        } else {
            button1.isSelected = false
            button2.isSelected = false
            button3.isSelected = true
        }

*我不确定是否也应该提及它,但我在尝试修复它时注意到了一些细微差别:

  • 我使用的图片是 PNG,具有透明背景
  • 使用这些透明图像,当取消选择 UIButton 时,整个按钮变为白色(同样,仅在与 Xcode 断开连接时...当我连接到 Xcode 时,它​​可以正常工作)
  • 如果我更改图像并添加背景,那么当取消选择按钮时,它仍然使用“已选择”图像

这是一个关于正在发生的事情的快速视频:https://www.dropbox.com/s/8pcl8lpkbgcr9l5/Example%20Issue.mov?dl=0(前半部分是模拟器连接到 Xcode,后半部分是在我停止运行 xcode 之后)

【问题讨论】:

  • 你能制作一个演示视频并在这里发布吗?
  • @Kudos 应要求:dropbox.com/s/8pcl8lpkbgcr9l5/Example%20Issue.mov?dl=0(前半部分是模拟器连接到 Xcode,后半部分是在我停止运行 xcode 之后)
  • 您是否更改了选中/默认状态下按钮的背景颜色?
  • @Kudos 不,唯一应该改变的是 UIButton 的背景图像
  • 你做错了。 Button State 不适用于 bgimage,但适用于 Button image。

标签: ios swift xcode


【解决方案1】:

我不知道您是如何构建源代码的,但我做了一个类似于您上面提供的视频的简单示例。

import UIKit
class DemoViewController: UIViewController {
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button3: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupButton()
    }
    
    func setupButton() {
        // Setup image for the buttons
        self.button1.setImage(UIImage(named: "book"), for: .normal)
        self.button2.setImage(UIImage(named: "headphone"), for: .normal)
        self.button3.setImage(UIImage(named: "img"), for: .normal)
        
        // setup background image for the buttons in the selected state
        // In normal the buttons do not have a background image
        self.button1.setBackgroundImage(UIImage(named: "bg"), for: .selected)
        self.button2.setBackgroundImage(UIImage(named: "bg"), for: .selected)
        self.button3.setBackgroundImage(UIImage(named: "bg"), for: .selected)
        
        // setup target action for the buttons
        self.button1.addTarget(self, action: #selector(showDetail(_:)), for: .touchUpInside)
        self.button2.addTarget(self, action: #selector(showDetail(_:)), for: .touchUpInside)
        self.button3.addTarget(self, action: #selector(showDetail(_:)), for: .touchUpInside)
        
        self.button1.isSelected = true // set default value for the button
    }
    
    
    @objc func showDetail(_ sender: UIButton) {
        // When user touch on the button then all buttons should be unselected
        // Then the target button should be selected
        button1.isSelected = false
        button2.isSelected = false
        button3.isSelected = false
        sender.isSelected = true
        var format = "";
        switch sender {
        case button1:
            format = "A"
        case button2:
            format = "B"
        default:
            format = "C"
        }
        
        // Show the detail screen
        self.performSegue(withIdentifier: "showDetail", sender: format)
    }
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        if let vc = segue.destination as? DetailViewController {
            vc.format = sender as! String
        }
    }
}

【讨论】:

  • 感谢您的回复!您是否遇到过类似的问题,它与正在运行的模拟器一起运行良好,但在模拟器分离时却不行?
  • 我一头雾水,不知道你在源代码中实现了什么。或者您可以向我们提供源代码是错误的,也许我们可以进一步调查您的问题?如果您确实喜欢我的示例,但它仍然不起作用,那么我认为根本原因在另一个地方。
  • @Diggory 我认为他正在尝试在状态更改的基础上更改 Button 的 bg。但这仅适用于按钮图像更改而不是 bgImage 更改
  • @Kudos -:)) 我不知道他的问题出在源代码中。我刚刚看了他的视频并做了一个例子,就像他上面所说的那样。因为按钮有 4 个你知道的基本状态:默认(正常)、突出显示、选中、禁用。在每种状态下,您都可以为其设置图像或图像背景。在源代码控制中,您只需为按钮设置正确的状态即可。
  • @Diggory 感谢您的帮助!不幸的是,这几乎与我已经拥有的实现完全相同(如果你看到视频,你会看到我已经成功地在附加模拟器的情况下工作)所以它并不能完全解决我的问题
猜你喜欢
  • 1970-01-01
  • 2013-02-24
  • 2020-03-29
  • 2019-03-04
  • 2022-07-29
  • 2017-01-20
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
相关资源
最近更新 更多