我有一个有两个屏幕的应用程序。在第一个屏幕中有一个按钮,通过模态segue打开第二个屏幕作为模态视图,它有一个UILabel。
我希望这个UILabel有一个特定的文本,这取决于用户点击按钮的次数(它们是提示:用户只能单击按钮并看到提示三次)。 每次单击按钮时,我正在做的是以下方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"TipModal"]) {
QuizTipViewController * detailViewController = (QuizTipViewController *) segue.destinationViewController;
detailViewController.delegate = self;
detailViewController.tipText = self.quiz.currentTip;
[detailViewController.numberTipsText setText:[NSString stringWithFormat:@"Pistas para la respuesta (usadas %ld de 3)", (long)self.quiz.tipCount]] ;
NSLog(@"%d", self.quiz.tipCount);
NSLog(@"%@", detailViewController.numberTipsText.text);
}
}
最后两个日志的输出如下:
2013-05-14 19:10:47.987 QuoteQuiz[1241:c07] 0
2013-05-14 19:10:47.989 QuoteQuiz[1241:c07] Hints (0 out of 3 used)
尽管如此,UILabel中的文字总是空的。
在模态窗口的视图控制器的.h文件中,我将UILabel定义为:
@property (strong, nonatomic) IBOutlet UILabel* numberTipsText;
我甚至在实现文件中创建了:
-(UILabel *)numberTipsText {
if (!_numberTipsText) {
_numberTipsText = [[UILabel alloc] init];
}
return _numberTipsText;
}
知道为什么会发生这种情况,拜托?
提前多多感谢!
答案 0 :(得分:0)
标签没有文字的原因是因为您将文本分配给
中创建的标签-(UILabel *)numberTipsText
吸气剂。然后,当从笔尖加载控制器的视图时, numberTipsText 属性将被从笔尖加载的标签覆盖,该标签不包含任何文本。解决方案是删除创建新UILabel的getter,然后:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"TipModal"]) {
QuizTipViewController * detailViewController = (QuizTipViewController *) segue.destinationViewController;
detailViewController.delegate = self;
detailViewController.tipText = self.quiz.currentTip;
//numberOfTipsText is a NSString property
detailViewController.numberOfTipsText = [NSString stringWithFormat:@"Pistas para la respuesta (usadas %ld de 3)", (long)self.quiz.tipCount];
}
}
在QuizTipViewController的viewDidLoad方法中:
- (void) viewDidLoad
{
[super viewDidLoad];
self.numberOfTipsLabel.text = self.numberOfTipsText;
}