简体   繁体   中英

How to disable a UIButton and send a UIAlertView message

I am trying to disable a menu button is the array is shows is empty.

This is my code.

@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
    }
}

And in ViewDidLoad()

likedMenuButton.userInteractionEnabled = false

I have managed to disable the button, but I want to send a message alerting the user why the button is disabled, otherwise, its a little confusing.

How would I go about doing this?

Thanks.

As by default the the user Interaction is true you need not to make it true

@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
        }
    }

Also check your array count , that is why your wrong condition is executing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM