我在ViewCotroller类中实现了一个方法,将消息设置为该类中的UILable。
-(void)setAuthenticationMessage:(NSString *)message{
//lblStatus is the UILabel
lblStatus.text = message;
}
我正从另一个类访问此方法来设置消息。虽然代码正确执行,但此消息未更新。我通过在另一个线程中执行此方法来尝试它。但那也没有成功。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
lblStatus
的内存管理属性是什么?你的命名惯例让我相信它与此有关。通常将实例变量称为:
_lblStatus
或self.lblStatus
答案 1 :(得分:0)
答案 2 :(得分:0)
使用委托设计模式(google it)。示例:
//In the updating class .h
@protocol LabelChangerDelegate <NSObject>
-(void)handleLabelChangeMessage;
@end
@property (assign, nonatomic) id <LabelChangerDelegate> delegate;
//In the updating class .m
-(void)changeLabelInOtherClass{
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(handleLabelChangeMessage)]) {
[self.delegate performSelector:@selector(handleLabelChangeMessage)];
}
} else {
NSLog(@"Delgate doesn't implement handleLabelChangeMessage");
}
//In the view controller .h
<LabelChangerDelegate>
-(void)viewDidLoad{
//set the delegate to self
}
//In the view controller .m
-(void)handleLabelChangeMessage{
self.label.text=@"I changed through delegation";
}
另一种选择是使用NSNotificationCenter