-3

I am making an iBeacon based app, but when the user adds a new beacon and pushes save, the screen takes a second to load back to the main viewController. If the user presses save more than once, it makes it so that the beacon is added more than once. Is there a way to deactivate a button after it is touched to avoid this problem?

- (IBAction)saveTag:(id)sender
{
    PFObject *newTag = [PFObject objectWithClassName:@"Tag"];

    newTag[@"Name"] = self.tagName.text;
    newTag[@"UUID"] = self.selectedBeacon.proximityUUID.UUIDString;
    newTag[@"Major"] = self.selectedBeacon.major;
    newTag[@"Minor"] = self.selectedBeacon.minor;                   

    if ([self.tagName.text isEqualToString:@""]) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Artemis" message:@"Cannot save tag without name" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

    } else {

        PFQuery *query = [PFQuery queryWithClassName:@"Tag"];
        [query whereKey:(@"UUID") equalTo:self.selectedBeacon.proximityUUID.UUIDString];
        [query whereKey:(@"Major") equalTo:self.selectedBeacon.major];
        [query whereKey:(@"Minor") equalTo:self.selectedBeacon.minor];

       [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
           if (!error) {
               NSLog(@"No errors found!");
    }
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128

3 Answers3

3

As answered here: How do I disable a UIButton?

self.buttonName.enabled = NO;
Community
  • 1
  • 1
Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
0

The documentation is very helpful for things like this. The trick is to look at the class you're interested in AND it's ancestor classes.

In this case the property, enabled, is a property of the ancestor class UIControl.

When you search on a class like UIButton in the Xcode docs, you'll see in the overview at the top a line "Inherits from", and a list of the ancestor classes.

First look over the methods and properties of the class you're looking at, and then click on the immediate ancestor class and read about ITS methods and properties. Sometimes the method you're after might be in a grandparent class or even further up the class hierarchy.

(Confession: I've been programming in Objective C/Cocoa/Cocoa touch pretty much full time since the beginning of 2007, and I still sometimes forget to check a class's ancestor classes for methods I'm looking for.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-2

You can disable all instances of UIButton by setting the enabled property to NO.

self.cancelButton.enabled = NO;
förschter
  • 740
  • 1
  • 7
  • 24