user1304842
user1304842

Reputation: 159

"setTitle" method not work

every one. I'm developing iphone app.

I try to change the Button's title in my code. When i click a button, then the title will change the other text.

So, i use setTitle method, but the app is killed.

The code is following.

-(IBAction)deleteProject:(id)sender{

    UIButton *button = (UIButton *) sender;
    if (addButton.enabled == YES ){

        addButton.enabled = NO ;

        //Here, the code is stopped.
        [button setTitle:@"Back" forstate:UIControlStateNormal];
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        [mainTableView setEditing:YES animated:YES];
    }

    else{

        addButton.enabled = YES;

        //Here, the code is stopped. V
        [button setTitle:@"Delete" forstate:UIControlStateNormal];
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        [mainTableView setEditing:NO animated:YES];
    }
}

Please help me.

Upvotes: 0

Views: 1877

Answers (3)

Global nomad
Global nomad

Reputation: 1022

You misspelled the setTitle:forState method in the UIButton class. Corrected spelling below for your convenience.

 -(IBAction)deleteProject:(id)sender{

    UIButton *button = (UIButton *) sender;
    if (addButton.enabled == YES ) // you could also use if (addButton.enabled)
    {

        addButton.enabled = NO ;

        [button setTitle:@"Back" forState:UIControlStateNormal];

        [mainTableView setEditing:YES animated:YES];
    }

    else // addButton.enabled == NO
    {

        addButton.enabled = YES;

        [button setTitle:@"Delete" forState:UIControlStateNormal];

        [mainTableView setEditing:NO animated:YES];
    }
 }

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

You get passed a sender parameter into your deleteProject: method. This parameter is type id, which can be anything.

You cast it to UIButton and try to use it as a UIButton.

The exception you get is [UIBarButtonItem setTitle:forState:]

This should tell you that sender isn't a UIButton at all, it's a UIBarButtonItem :)

You can test this but putting this code at the start of your deleteProject: method.

if (NO == [sender isKindOfClass:[UIButton class]]) {
    NSLog(@"Oh no, sender isn't a button, it's a %@", [sender class]);
}

Upvotes: 3

Svein Halvor Halvorsen
Svein Halvor Halvorsen

Reputation: 2494

Capital S in setTitle:forState:

Also, use == for comparison, = for assignment. An assignment is always true.

Upvotes: 0

Related Questions