如何使用 switch 语句来查找 UIButton 是否被按下?

发布于 2024-12-16 13:41:04 字数 468 浏览 0 评论 0原文

我正在制作一个 ios 应用程序,但在使用 switch 语句来查看 UIButton 元素是否被按下时遇到问题。 这就是我希望最终产品发挥作用的方式:我有多个未着色的图像(我所说的未着色是指白色,即 UIImage)。当点击未着色的图像时,会打开一个带有彩色框的子视图(UIButtons,其中 24 个,每个都有单独的颜色。)。当选择彩色框按钮并按下工具栏上的后退按钮时,子视图将关闭,原始视图将重新显示,其中未着色的图像(选择打开子视图的图像)现在已使用子视图中选择的所需颜色进行着色。

我想使用 switch 语句来查找选择了哪个未着色图像以及哪种颜色(所有 UIButton 元素)。我不知道在 switch 语句中放置什么作为表达式,因为我正在处理 UIButtons。 switch 语句的其余部分比较 UIButton 元素的值以查看它是否等于 YES(按下按钮时),如果是,则返回一个字符串。我还想知道如何将 IBAction 连接到 UIImage (因此当点击图像时,子视图将打开)。

I’m making an ios application and am having trouble using a switch statement to see if a UIButton element was pressed.
This is who I want the end product to work: I have multiple uncolored images (by uncolored I mean white, a UIImage). When an uncolored image is tapped a subview opens with colored boxes (UIButtons, 24 of them, each with individual colors.). When a colored box button is selected and the back button on the toolbar is pressed, the subview closes and the original view re-appears with the uncolored image (the one selected to open the subview) now colored with the desired color selected in the subview.

I want to use a switch statement to find which uncolored image and which color was selected (all UIButton elements). I do not know what to put as an expression in the switch statement because I’m dealing with UIButtons. The rest of the switch statement compares the value of the UIButton element to see if it’s equal to YES (when the button is pressed), and if it is, it returns a string. I also want to know how to connect an IBAction to a UIImage (so when the images are tapped a subview opens).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2024-12-23 13:41:04

我对 iOS 开发有点生疏,但您可能可以执行以下操作:

将按钮设置为相同的事件处理程序,并使用 sender 属性获取按钮的标签元素,您可以为每个按钮指定该标签元素。

- (IBAction) doStuff:(id) sender {
UIButton *button = (UIButton*) sender;
switch(button.tag)
{
   //do stuff
}

如果这不适合您,您可以使用您认为合适的任何按钮属性来区分它们,例如标题、标题颜色等。

为了获得最佳实践,我建议您在尝试将发送者转换为对象之前检查发送者是否属于 UIButton 类型。

I'm a little rusty on iOS development but you could probably do the following:

Set the buttons to the same event handler and use the sender attribute to get to the tag element of the button which you can specify to each button.

- (IBAction) doStuff:(id) sender {
UIButton *button = (UIButton*) sender;
switch(button.tag)
{
   //do stuff
}

If this doesn't work out for you, you can use any of the button properties you see fit to differentiate between them such as title, title color and so on.

For best practices i'd advise you to also check if the sender is of type UIButton before trying to cast it into an object.

⊕婉儿 2024-12-23 13:41:04

对于Swift 3.0,我们不需要再观察标签了。只需保留对按钮(IBOutlet 或某些私有变量)的引用,然后使用标识符模式打开按钮本身。

import UIKit

class Foo {
    // Create three UIButton instances - can be IBOutlet too
    let buttonOne = UIButton()
    let buttonTwo = UIButton()
    let buttonThree = UIButton()

    init() {
        // Assign the same selector to all of the buttons - Same as setting the same IBAction for the same buttons
        [buttonOne, buttonTwo, buttonThree].forEach{(
            $0.addTarget(self, action: Selector(("buttonTapped")), for: .touchUpInside)    
        )}
    }

    func buttonTapped(sender: UIButton) {
        // Lets just use the Identifier Pattern for finding the right tapped button
        switch sender {
        case buttonOne:
            print("button one was tapped")
        case buttonTwo:
            print("button two was tapped")
        case buttonThree:
            print("button three was tapped")
        default:
            print("unkown button was tapped")
            break;
        }
    }
}

// Example
let foo = Foo()
foo.buttonTapped(sender: foo.buttonOne)

For Swift 3.0, we do not need to observe the tags any more. Just keep a reference to your buttons (IBOutlet or some private variable), and switch on the button itself, using the Identifier Pattern.

import UIKit

class Foo {
    // Create three UIButton instances - can be IBOutlet too
    let buttonOne = UIButton()
    let buttonTwo = UIButton()
    let buttonThree = UIButton()

    init() {
        // Assign the same selector to all of the buttons - Same as setting the same IBAction for the same buttons
        [buttonOne, buttonTwo, buttonThree].forEach{(
            $0.addTarget(self, action: Selector(("buttonTapped")), for: .touchUpInside)    
        )}
    }

    func buttonTapped(sender: UIButton) {
        // Lets just use the Identifier Pattern for finding the right tapped button
        switch sender {
        case buttonOne:
            print("button one was tapped")
        case buttonTwo:
            print("button two was tapped")
        case buttonThree:
            print("button three was tapped")
        default:
            print("unkown button was tapped")
            break;
        }
    }
}

// Example
let foo = Foo()
foo.buttonTapped(sender: foo.buttonOne)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文