Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3457794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:54:14+00:00 2026-05-18T09:54:14+00:00

This is very strange. My view is receiving a notification with an index number

  • 0

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];
            });
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:54:15+00:00Added an answer on May 18, 2026 at 9:54 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find this very strange, must be something I'm doing wrong, but still... I'm
This is a very strange problem, and I just hope that I can clearly
This is very strange. An MVC View refuses to recognize the System.Speech namespace. What's
I am having a very strange problem, I have this file for User#show view:
This very strange. When I resize a WinForms dialog the controls are flickering very
This is very strange, but when I add the mysql_real_escape_string it doesn't load the
This is very very strange. Basically, i'm using ASP.NET. What I want is to
This problem is very strange and I'm hoping someone can help me. For the
It was very strange when I saw this on debugging my application. int iTag
it is very strange, because this error doesn't happen all the time... I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.