在 Xcode 4.2 问题中设置 IBOutlet 属性
我正在使用基于导航的 iPhone 应用程序,我在内部视图中定义了 IBOutlet 属性并合成了它,
问题是当我想在推送新视图控制器之前设置 IBOutlet 值时,该值将不会被设置。以下是代码片段:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MealDetailViewController *mealViewController = [[MealDetailViewController alloc] initWithNibName:@"MealDetailViewController" bundle:nil];
MealsModel *model = (MealsModel *)[_items objectAtIndex:indexPath.row];
NSLog(model.Name);// here it writes the name right as a string
//mealViewController.lblName.text=model.Name;
[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];
[self.navigationController pushViewController:mealViewController animated:YES];
[mealViewController release];
}
在以前版本的 Xcode 中我没有遇到这些问题。
I am using a navigation based iPhone application, and I defined an IBOutlet propert in the inner view and synthesized it,
The issue is when I want to set the IBOutlet value before pushing the new view controller, the value won't be set. Here is a snippet from the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MealDetailViewController *mealViewController = [[MealDetailViewController alloc] initWithNibName:@"MealDetailViewController" bundle:nil];
MealsModel *model = (MealsModel *)[_items objectAtIndex:indexPath.row];
NSLog(model.Name);// here it writes the name right as a string
//mealViewController.lblName.text=model.Name;
[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];
[self.navigationController pushViewController:mealViewController animated:YES];
[mealViewController release];
}
I didn't face like these issues in the previous versions of Xcode.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与创建视图控制器的视图的时间有关。
当你这样做时
,你还没有加载它的视图,所以lblName将为
nil
。尝试
(a)显式请求视图,触发要创建的子视图:
或
(b)让导航控制器为您创建视图
或
(c)将mealViewController中的值存储为属性
MealViewController.h
MealViewcontroller.m
和不要直接设置标签,而是设置属性。
然后,在 newWillAppear 中,设置它们
(c) 是更正确的方法 - 如果视图控制器的视图因内存不足警告而卸载,则 (a) 和 (b) 都会失败。
It's to do with when your view controller's views are being created.
When you do
you haven't loaded it's view yet so lblName will be
nil
.Try either
(a) Explicitly asking for the view, triggering the subviews to be created :
or
(b) Letting the navigation controller create the views for you
or
(c) Store the values in the mealViewController as properties
MealViewController.h
MealViewcontroller.m
and instead of setting the label directly, set the properties instead.
Then, in your newWillAppear, setting them
(c) is the more correct way to do it - (a) and (b) both fail if your view controller's view is unloaded by a low memory warning.
我要做的是将 *Meal 对象传递给 MealDetailViewController。然后使用它在 MealDetailViewController 中设置标签等。一旦我遇到类似的问题,发现我正在初始化接收类的 ViewDidLoad 中的 UILabel 属性,因此它会覆盖任何传入的文本。
What I would do is pass the *Meal object to the MealDetailViewController. Then use that to set the labels and such in the MealDetailViewController. Once I had a similar problem and found that I was initializing the property, a UILabel in ViewDidLoad of my receiving class so it was overwriting any passed in text.