通过委托更改IBOutlet UILabel的文本

时间:2014-05-30 13:48:34

标签: objective-c uilabel

我正在尝试更改通过Storyboard创建的UILabel的文本。我有两个ViewControllers,FirstViewController和SecondViewController。我解散了SecondViewController,在SecondViewController中调用该方法,在FirstViewController中调用委托方法,但UILabel为nil。 知道为什么吗?

SecondViewController

-(void)back {
    [self.navigationController popToRootViewControllerAnimated:YES];  
    [self.delegate points:@"10"];
}

FirstViewController

-(void)points:(NSString *)point {
     labelPoints.text = [NSString stringWithFormat:@"Points: %@", point]; 
}

3 个答案:

答案 0 :(得分:0)

你的FirstViewController是rootViewController吗?否则推送到rootViewController将从堆栈中删除First ViewController。从Second View Controller首先调用delegate,然后调用popViewController,如下所示。

 -(void)back {
    [self.delegate points:@"10"];
    [self.navigationController popViewControllerAnimated:YES];  

 }

答案 1 :(得分:0)

在设置值之前,您正在弹出VC。我会像你这样从你的委托对象中弹出:

-(void)back { 
     [self.delegate points:@"10"];
    }

-(void)points:(NSString *)point {
    labelPoints.text = [NSString stringWithFormat:@"Points: %@", point];
    [self.navigationController popToViewController:self animated:YES];
}

如果这不能解决问题,那么除了这个问题之外你可能还有另一个问题,并且必须发布更多代码。

答案 2 :(得分:0)

UILabel中设置文本的延迟对我有用

例如,当委托调用这样的方法时:

[self.delegate selectedAttendee:dictAttendee];
[self.navigationController popViewControllerAnimated:YES];

这对我没用:

-(void)selectedAttendee:(NSDictionary *)dictAttendee
{
  lblMessageTo.text = [dictAttendee objectForKey:@"name"];
}

解决方案

-(void)selectedAttendee:(NSDictionary *)dictAttendee
{
  [self performSelector:@selector(addTextInMessageTo:) withObject:dictAttendee afterDelay:0.3];
}

-(void)addTextInMessageTo:(NSDictionary *)dictAttendee
{
   lblMessageTo.text = [dictAttendee objectForKey:@"name"];
}