dubbeat
dubbeat

Reputation: 7847

oddly a uibuttons text label won't change

This is very strange. My view is receiving a notification with an index number that I use to find a UIbutton by tag and then change its text label. The only trouble is the text will not change. I've put in break points and the code is being executed.

Is there some peculiarity with variable scopes when using notifications?

- (void)receiveEvent:(NSNotification *)notification {


    int pass = [[[notification userInfo] valueForKey:@"index"] intValue];
    NSLog(@"button index is %i", pass);

    UIButton *changebut = (UIButton *) [self.view viewWithTag:pass];
    ButtonStatusModel *m= [self.buttoneventpendingarray objectAtIndex:pass];
    NSLog(@"current but status = %i",m.btnstatus);
    if (m.btnstatus==3 ) {
        //this should change the label but it does not;
        [changebut setTitle:@"playing" forState:UIControlStateNormal];
        m.btnstatus=2;
        NSLog(@"should set text to 'playing");
        //[engine addActionToQue:buttonid actionSample:actionsample action:1];

    }else if (m.btnstatus==1) {

        //this should change the label but it does not;
        [changebut setTitle:@"idle" forState:UIControlStateNormal];
        NSLog(@"should set text to 'idle");
        m.btnstatus=0;
        //[engine addActionToQue:buttonid actionSample:actionsample action:0];

    }


}

EDIT 1 Thanks to commenters I've determined that even though my reciveevent function in on my main view controller the function is not being executed from the main thread (dont really understand this) and so thats why the button label will not change.

I used the following code to determine if it was the main thread or not. I think now to change the button label I need to call perform selector on main thread? Is this the correct thing to do? If so I need to pass a function 2 variables, I dont see how perform selector on main thread accomodates this.

-(void) updatebutton:(int)tag changeto:(int) val
{

    if ([NSThread isMainThread]) {
         {
             NSLog(@"is main thread");
        }
    } else {
         NSLog(@"is not thread");
        [self performSelectorOnMainThread:@selector( /*function with 2 parameters here*/) withObject:nil waitUntilDone:YES];

    }

}

EDIT 3*

Using blocks instead of main selector helped me out of my problem.

I really don't understand the whole main thread though. I assumed that if code was in my view controller that any executed code would be performed on the main thread. That seems to be big fat wrong. I would appreciate a little enlightenment on the matter.

Heres what I used anyway

-(void) updatebutton:(int)tag changeto:(int) val
{
    UIButton *changebut = (UIButton *) [self.view viewWithTag:tag];

    if ([NSThread isMainThread]) {
         {
             NSLog(@"is main thread");

             if (val==2) {
                 [changebut setTitle:@"playing" forState:UIControlStateNormal];

             }

             if (val==0) {

                     [changebut setTitle:@"idle" forState:UIControlStateNormal];
             }

        }
    } else {
         NSLog(@"is not thread");
        //[self performSelectorOnMainThread:@selector(updatebutton::) withObject:nil waitUntilDone:YES];

        if (val==2) {

            dispatch_async(dispatch_get_main_queue(), ^{
                [changebut setTitle:@"playing" forState:UIControlStateNormal];
            });
        }

        if (val==0) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [changebut setTitle:@"idle" forState:UIControlStateNormal];
            });
        }

    }

}

Upvotes: 1

Views: 2486

Answers (2)

Björn Marschollek
Björn Marschollek

Reputation: 9999

Is -receiveEvent called on another than the main thread? UI changes must be performed on the main thread.

Upvotes: 0

Knodel
Knodel

Reputation: 4389

Try putting

  [changebut setTitle:@"idle" forState:UIControlStateNormal];

in the -viewDidAppear method. If it doesn't work either, make sure that all the outlets are connected in IB. Moreover, try adding a few spaces as the button title in your .xib file (if you have one).

Upvotes: 2

Related Questions