Objective-C Storyboard:UILabel没有在UIViewController中使用NSString值进行更新

时间:2014-04-21 03:23:37

标签: ios objective-c uiview uilabel

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

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

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

问题:

  1. 为什么self.nameLabel.text可以轻松显示self.nameLabel.text = @"Maria Llewellyngot";,并且在使用(null)时返回了self.nameLabel.text = [NSString stringWithFormat:@"%@", [self.userInfo valueForKey:@"groupName"]];个值,并且看到groupName已填充使用retrieveFromParse()方法中的值。
  2. UserViewController.h

      

    @property(非原子,强)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 :(得分:0)

我认为valueForKey在这里返回的是NSArray,而不是NSString。因此,日志语句中的()。要使用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]);

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