swift - 即使我在 swift 中使用了正确的标签,也无法访问我想要的按钮标题

标签 swift button

我想创建一个带有 x 和 o 的井字游戏风格的棋盘。第一部分有效并成功更改了按钮代码。

   func buttonAction(sender:UIButton!)
    {
        print("Button \(sender.tag) tapped")
        var tag = sender.tag
        print(sender.currentTitle!)
        if sender.currentTitle! == "" {


            sender.titleLabel!.font = UIFont(name: "chalkduster", size:  fontSizeTag)
            sender.setTitleColor(UIColor.redColor(), forState: .Normal)
            sender.setTitle("X", forState: UIControlState.Normal)

        } else if sender.currentTitle! == "X"  {
            sender.titleLabel!.font = UIFont(name: "chalkduster", size:  fontSizeTag)
            sender.setTitleColor(UIColor.greenColor(), forState: .Normal)
            sender.setTitle("O", forState: UIControlState.Normal)

        }else if sender.currentTitle! == "O"  {
            sender.titleLabel!.font = UIFont(name: "chalkduster", size:  fontSizeTag)
            //sender.setTitleColor(UIColor.greenColor(), forState: .Normal)
            sender.setTitle("", forState: UIControlState.Normal)

        }
    }

这给了我正确的按钮,但无法访问其中已有的标题。

   @IBAction func deleteAnswers(sender: UIBarButtonItem) {

      var totalBoxes = getTotalNumberOfBoxesInGrid()
        var tag = Int()

        for tag = 1; tag <= totalBoxes; tag++ {

                let button = self.view.viewWithTag(tag) as? UIButton
                button!.setTitle("", forState: UIControlState.Normal)
        }
    }

事实上,在第二部分中,将空字符串“”(无标题)更改为字母,例如“P”,只是在现有标题上打印一个“P”。我如何获得原始标题?

最佳答案

我醒来时脑子里就浮现出这个解决方案,它确实有效。然而,它是一个解决方案,而不是严格意义上的问题的答案:即如何访问使用 send.setTitle 设置的按钮标题。当“发送者”仅用于获取标签#并且按照我在下面设置的方式设置按钮标题时,删除和访问信息所需的其他功能就起作用了。

func buttonAction(sender:UIButton!)
{
        print("Button \(sender.tag) tapped")
        var tag = sender.tag
        print(sender.currentTitle!)

 if var button = self.view.viewWithTag(tag) as? UIButton {

    if button.currentTitle! == "" {


        button.titleLabel!.font = UIFont(name: "chalkduster", size:  fontSizeTag)
        button.setTitleColor(UIColor.redColor(), forState: .Normal)
        button.setTitle("X", forState: UIControlState.Normal)
        myUserValuesDictionary[globalAppD.newPuzzle.name]![tag] = 1
        } else if button.currentTitle! == "X"  {
        button.titleLabel!.font = UIFont(name: "chalkduster", size:  fontSizeTag)
        button.setTitleColor(UIColor.greenColor(), forState: .Normal)
        button.setTitle("O", forState: UIControlState.Normal)
        myUserValuesDictionary[globalAppD.newPuzzle.name]![tag] = 2
        }else if button.currentTitle! == "O"  {
        button.titleLabel!.font = UIFont(name: "chalkduster", size:  fontSizeTag)
        //button.setTitleColor(UIColor.greenColor(), forState: .Normal)
        button.setTitle("", forState: UIControlState.Normal)
        myUserValuesDictionary[globalAppD.newPuzzle.name]![tag] = 0
        }
    }
}

关于swift - 即使我在 swift 中使用了正确的标签,也无法访问我想要的按钮标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36540874/

相关文章:

java - 设置按钮可见性的计时器

android - 错误 :(10) Error parsing XML: not well-formed (invalid token)

css - 输入按钮移除 "shadow"

带有 UIView 的 iOS UITableView 自定义页脚

swift - 条形按钮项目背景颜色

ios - 检测哪个字符在 Swift 中被退格

ios - 在 SWIFT 中推送之前如何加载 View ?

android - 在 Android 按钮中垂直居中超大文本

java - Android:单击按钮时切换相机

ios - 从 Firebase 解析数据时无法成功重新加载 UITableView 元素