【问题标题】:How to disable a UIButton and send a UIAlertView message如何禁用 UIButton 并发送 UIAlertView 消息
【发布时间】:2016-02-24 06:03:34
【问题描述】:

我正在尝试禁用菜单按钮,因为数组显示为空。

这是我的代码。

@IBAction func likedmenubuttontouched(sender: AnyObject) {

    if Globals.likedArray.isEmpty {
        likedMenuButton.userInteractionEnabled = false
        let ac = UIAlertController(title: "No liked quotes yet", message: "No liked quotes have been chosen, go explore!", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
        return
    } else {
        likedMenuButton.userInteractionEnabled = true
    }
}

在 ViewDidLoad() 中

likedMenuButton.userInteractionEnabled = false

我已设法禁用该按钮,但我想向用户发送一条消息,提醒用户该按钮为何被禁用,否则会有点混乱。

我该怎么做呢?

谢谢。

【问题讨论】:

  • 我认为条件相反
  • 哎呀,我忘了说我把这段代码 likeMenuButton.userInteractionEnabled = false is viewdidload()
  • 为什么要设置 likeMenuButton.userInteractionEnabled = false
  • 啊,我明白你的第一条评论是什么意思,我会编辑我的问题
  • 首选方式是likedMenuButton.enabled = false 而不是userInteractionEnabled = false

标签: ios arrays swift uibutton uialertview


【解决方案1】:

由于默认情况下用户交互为真,您无需使其为真

@IBAction func likedmenubuttontouched(sender: AnyObject) {


        if Globals.likedArray.isEmpty {

            let ac = UIAlertController(title: "No liked quotes yet", message: "No liked quotes have been chosen, go explore!", preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
            presentViewController(ac, animated: true, completion: nil)
        } else {
            //segue to the other view
        }
    }

还要检查您的数组计数,这就是执行错误条件的原因

【讨论】:

  • 似乎可以工作,但是当没有引号时出现“致命错误:数组索引超出范围”?
  • 它显示是因为您正在访问数组中不存在的索引处的对象。如果您正在访问数组中的对象,请检查代码。
  • 我相信这是因为另一个视图控制器的 viewdidload() 中的这一行 - self.favouritesLabel.text = Globals.likedArray[currentlikedArrayIndex] as String -
  • 是的。因为数组不包含任何对象,但您仍在访问该索引..
  • 当我点击按钮时它访问它,即使它什么也没有,我该如何阻止它?
猜你喜欢
  • 2013-03-08
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多