简体   繁体   中英

Objective-C Storyboard: UILabel is not updating with NSString value in UIViewController

I have a query that I am running in the background with Parse.com SDK. Once, I have the NSArray populated I pass the indexed data to the label.

The label I see that the viewDidLoad method is completed before a actual value is passed to self.nameLabel.text (the label is currently commented out in viewDidLoad). I am thinking it is related to threading as the query takes time to execute.

Once the retrievedFromParse() method is complete it should update the UILabel self.nameLabel.text to display the returned value "Monstars" as it is displayed in the log output.

Questions:

  1. Why is the self.nameLabel.text displays easily with self.nameLabel.text = @"Maria Llewellyngot"; and there's a (null) value returned when using self.nameLabel.text = [NSString stringWithFormat:@"%@", [self.userInfo valueForKey:@"groupName"]]; and see that the groupName is populated with a value in retrievedFromParse() method.

UserViewController.h

@property (nonatomic, strong) NSArray *userInfo;

UserViewController.m

#import "UserProfileViewController.h"


@interface UserProfileViewController ()

@end

@implementation UserProfileViewController
@synthesize userInfo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelector:@selector(retrievedFromParse)];

    UIColor* mainColor = [UIColor colorWithRed:28.0/255 green:158.0/255 blue:121.0/255 alpha:1.0f];
    UIColor* imageBorderColor = [UIColor colorWithRed:28.0/255 green:158.0/255 blue:121.0/255 alpha:0.4f];

    NSString* fontName = @"Avenir-Book";
    NSString* boldItalicFontName = @"Avenir-BlackOblique";
    NSString* boldFontName = @"Avenir-Black";

    self.nameLabel.textColor =  mainColor;
    self.nameLabel.font =  [UIFont fontWithName:boldItalicFontName size:18.0f];
    //self.nameLabel.text = @"Maria Llewellyngot";
    //self.nameLabel.text = [NSString stringWithFormat:@"%@", [self.userInfo valueForKey:@"groupName"]];

    NSLog(@"Print group name 2%@", [self.userInfo valueForKey:@"groupName"]);

    self.usernameLabel.textColor =  mainColor;
    self.usernameLabel.font =  [UIFont fontWithName:fontName size:14.0f];
    self.usernameLabel.text = @"@llewellyngot";


    self.scrollView.contentSize = CGSizeMake(320, 590);
    self.scrollView.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)retrievedFromParse{
    NSLog(@"User retrievedFromParse method called");

    PFQuery *groupQuery = [PFQuery queryWithClassName:@"Group"];
    [groupQuery whereKey:@"groupName" equalTo:@"Monstars"];
    groupQuery.cachePolicy = kPFCachePolicyNetworkElseCache;

    NSLog(@"I was able to perform the group query");

    // Run the query
    [groupQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {

            userInfo = [[NSArray alloc] initWithArray:objects];
            NSLog(@"I see the data from userInfo: %@", userInfo);
            NSLog(@"Print group name 1%@", [self.userInfo valueForKey:@"groupName"]);

            [self performSelector:@selector(displaTextData)];

            //[self.view setNeedsDisplay];

        }
    }];
}

- (void)displaTextData{
    NSLog(@"Print group name 3%@", [self.userInfo valueForKey:@"groupName"]);
    self.nameLabel.text = [NSString stringWithFormat:@"%@", [self.userInfo valueForKey:@"groupName"]];
    //[self.nameLabel.text performSelectorOnMainThread : @ selector(setText: ) withObject:[self.userInfo valueForKey:@"groupName"] waitUntilDone:YES];
    //self.nameLabel.text = @"Maria Llewellyngot";

}

@end

Here is a log of the program flow.

2014-04-20 14:40:24.877 App[6340:a0b] User retrievedFromParse method called
2014-04-20 14:40:24.877 App[6340:a0b] I was able to perform the group query
2014-04-20 14:40:24.880 App[6340:a0b] Print group name 2(null)
2014-04-20 14:40:25.442 App[6340:a0b] I see the data from userInfo: (
    "<Group:9xxxxxxxxxxx:(null)> {\n    createdBy = \"<PFUser:xxxxxxxxx>\";\n    groupName = Monstars;\n    image = \"<PFFile: 0xa9c144x>\";\n}"
)
2014-04-20 14:40:25.447 App[6340:a0b] Print group name 1(
    Monstars
)
2014-04-20 14:40:25.448 App[6340:a0b] Print group name 3(
    Monstars
)

Please note I have some lines commented of other methods I tried.

I believe that valueForKey , here, is returning an NSArray , and not an NSString . Hence the ( an ) in the log statements. To get the string use objectAtIndex: on the array received with valueForKey .

Sample :

self.nameLabel.text = [NSString stringWithFormat:@"%@", [[self.userInfo valueForKey:@"groupName"] objectAtIndex:0]];


self.nameLabel.text = [NSString stringWithFormat:@"%@", [[self.userInfo valueForKey:@"groupName"] firstObject]];

Also, log the class name, to make sure what datatype it is:

NSLog(@"object class : %@", [[self.userInfo valueForKey@"groupName"] class]);

If it says an array, then my assumption is right.

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