Ios Objective-C情节提要:UILabel未使用UIViewController中的NSString值进行更新

Ios Objective-C情节提要:UILabel未使用UIViewController中的NSString值进行更新,ios,objective-c,uiview,uilabel,Ios,Objective C,Uiview,Uilabel,我有一个查询,我正在后台运行Parse.com SDK。一旦填充了NSArray,我就会将索引数据传递给标签 我看到的标签是viewDidLoad方法在实际值传递到self.namelab.text之前完成的(标签当前在viewDidLoad中被注释掉)。我认为这与线程有关,因为执行查询需要时间 retrievedFromParse()方法完成后,应更新UILabel self.nameLabel.text以显示返回值“Monstars”,该值在日志输出中显示 问题: 为什么self.namel

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

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

retrievedFromParse()方法完成后,应更新UILabel self.nameLabel.text以显示返回值“Monstars”,该值在日志输出中显示

问题:

  • 为什么
    self.namelab.text
    self.namelab.text=@“Maria Llewellyngot”一起轻松显示self.namelab.text=[NSString stringWithFormat:@“%@”,[self.userInfo valueForKey:@“groupName”]时返回一个
    (null)
    并查看
    groupName
    在retrievedFromParse()方法中填充了一个值
  • 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
    )
    
    2014-04-20 14:40:24.877应用程序[6340:a0b]调用了从解析方法检索的用户
    2014-04-20 14:40:24.877应用程序[6340:a0b]我能够执行组查询
    2014-04-20 14:40:24.880应用程序[6340:a0b]打印组名称2(空)
    2014-04-20 14:40:25.442应用程序[6340:a0b]我从用户信息中看到数据:(
    {\n createdBy=\“\”;\n groupName=Monstars;\n image=\“\”;\n}
    )
    2014-04-20 14:40:25.447 App[6340:a0b]打印组名称1(
    主教
    )
    2014-04-20 14:40:25.448 App[6340:a0b]打印组名称3(
    主教
    )
    

    请注意,我对我尝试的其他方法进行了一些注释。

    我认为
    valueForKey
    在这里返回的是
    NSArray
    ,而不是
    NSString
    。因此,日志语句中的
    一个
    。要获取字符串,请对使用
    valueForKey
    接收的数组使用
    objectAtIndex:

    样本:

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

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

    在回答第一个问题时,
    [self.userInfo valueForKey:@“groupName”]
    返回nil,这可能是因为self.userInfo为nil,或者userInfo中没有名为“groupName”的条目。而且似乎在
    retrievedFromParse
    之前调用了
    viewDidLoad
    (这是设置
    self.userInfo
    的地方)。此外,您应该始终使用
    self.userInfo
    引用该变量。在使用ARC时,您可以不必放弃
    self.
    ,但这样做并不完全安全。谢谢您的回复。这让我难堪了几天。带有类打印的NSLOG将是一个有用的提示,供将来参考。