我有一个查询,我正在使用Parse.com SDK在后台运行。 一次,我填充了NSArray将索引数据传递给标签。

我看到的标签是,在将实际值传递给self.nameLabel.text之前,该viewDidLoad方法已完成(该标签当前在viewDidLoad中已被注释掉)。 我认为这与线程有关,因为查询需要时间来执行。

一旦retriatedFromParse()方法完成,它应该更新UILabel self.nameLabel.text以显示返回值“ Monstars”,因为它显示在日志输出中。

问题:

  1. 为什么self.nameLabel.text显示器很容易与self.nameLabel.text = @"Maria Llewellyngot"; 当使用self.nameLabel.text = [NSString stringWithFormat:@"%@", [self.userInfo valueForKey:@"groupName"]];返回一个(null)self.nameLabel.text = [NSString stringWithFormat:@"%@", [self.userInfo valueForKey:@"groupName"]]; 并看到groupName填充了groupName ()方法中的值。

UserViewController.h

@属性(非原子的,强的)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

这是程序流程的日志。

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
)

请注意,我在某些行中评论了我尝试过的其他方法。

===============>>#1 票数:0 已采纳

我相信valueForKey在这里返回一个NSArray ,而不是NSString 因此,log语句中的( an ) 要获取字符串,请在通过valueForKey接收到的数组上使用objectAtIndex: valueForKey

样品:

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


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

另外,记录类名称,以确保它是什么数据类型:

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

如果它说一个数组,那么我的假设是正确的。

  ask by JetSet translate from so

未解决问题?本站智能推荐: