iPhone UILabel 在首次加载时未更新
我有一个 UILabel
,它不会在第一次加载时更新,但会在每次其他后续加载时正确加载。
这是代码片段。再次,正如我所说,只有第一次加载时,它才会显示 xib 中标签中指定的内容,即 label
,第二次加载时,它将加载字符串 label.text= @ “玻璃、纸张、罐头、纺织品、卡片、鞋子和书籍”;
。
-(IBAction)showDetails:(id)sender{
NSLog(@"Annotation Click");
self.userProfileVC.title=((UIButton*)sender).currentTitle;
if([((UIButton*)sender).currentTitle isEqualToString:@"Asda"])
self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books";
[UIView beginAnimations:@"Curl Page Down" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.view cache:YES];
//Adjust the Subview by butting up to status bar
self.userProfileVC.view.frame = CGRectMake(0, 0, 320, 480);
//Add userProfileVC as a subview
[self.view addSubview:userProfileVC.view];
[UIView commitAnimations];
}
I have a UILabel
that does not update on first load but does load correctly on every other subsequent load.
Here is the snippet of code. Again, as I said, on the first load only it will display what's specified in the label in the xib, i.e label
, and the second time it will load the string label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books";
.
-(IBAction)showDetails:(id)sender{
NSLog(@"Annotation Click");
self.userProfileVC.title=((UIButton*)sender).currentTitle;
if([((UIButton*)sender).currentTitle isEqualToString:@"Asda"])
self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books";
[UIView beginAnimations:@"Curl Page Down" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.view cache:YES];
//Adjust the Subview by butting up to status bar
self.userProfileVC.view.frame = CGRectMake(0, 0, 320, 480);
//Add userProfileVC as a subview
[self.view addSubview:userProfileVC.view];
[UIView commitAnimations];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信当你第一次访问 self.userProfileVC.label 时,label 仍然是 nil 因为视图本身没有尚未加载。快速解决方法是,您必须首先通过将
self.userProfileVC.view;
语句放在showDetails
方法的开头来加载视图。I believe that at the time you access
self.userProfileVC.label
at the first time, thelabel
is stillnil
because the view itself hasn't been loaded yet. The quick fix is that you must load the view first by puttingself.userProfileVC.view;
statement at the beginning of theshowDetails
method.大概当您单击 UIButton 时会调用此代码。因此,在第一次 UI 加载时,我不希望调用您的方法(因为没有按下任何按钮)。为什么不尝试在
viewDidLoad
或类似内容中设置默认文本?例如:
Presumably this code is called when you click your UIButton. So at first UI load I wouldn't expect your method to get called (as no button was pressed). Why don't you try setting up the default text in
viewDidLoad
or similar?e.g. something along the lines of:
我真是个白痴,我改变了代码的顺序......在标签之前先加载视图。卫生部!
代码片段如下:-(
IBAction)showDetails:(id)sender{
I am such an idiot I have changed the order of my code....load the view first before the labels. DOH!
Heres the snippet:
-(IBAction)showDetails:(id)sender{