在 Xcode 4.2 问题中设置 IBOutlet 属性

发布于 2024-12-23 18:50:07 字数 846 浏览 1 评论 0原文

我正在使用基于导航的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-12-30 18:50:07

这与创建视图控制器的视图的时间有关。

当你这样做时

[[mealViewController lblName] setText:model.Name];

,你还没有加载它的视图,所以lblName将为nil

尝试

(a)显式请求视图,触发要创建的子视图:

[mealViewcontroller view];
[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];

(b)让导航控制器为您创建视图

[self.navigationController pushViewController:mealViewController animated:YES];

[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];

(c)将mealViewController中的值存储为属性

MealViewController.h

@property (nonatomic, copy) NSString *lblNameString;
@property (nonatomic, copy) NSString *txtDescString;

MealViewcontroller.m

@synthesize txtDescString, lblNameString;

和不要直接设置标签,而是设置属性。

[mealViewController setLblNameString:model.Name];
[mealViewController setTxtDescString:model.Description];

然后,在 newWillAppear 中,设置它们

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    lblName.text = lblNameString;
    txtDesc.text = txtDescString;
}

(c) 是更正确的方法 - 如果视图控制器的视图因内存不足警告而卸载,则 (a) 和 (b) 都会失败。

It's to do with when your view controller's views are being created.

When you do

[[mealViewController lblName] setText:model.Name];

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 :

[mealViewcontroller view];
[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];

or

(b) Letting the navigation controller create the views for you

[self.navigationController pushViewController:mealViewController animated:YES];

[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];

or

(c) Store the values in the mealViewController as properties

MealViewController.h

@property (nonatomic, copy) NSString *lblNameString;
@property (nonatomic, copy) NSString *txtDescString;

MealViewcontroller.m

@synthesize txtDescString, lblNameString;

and instead of setting the label directly, set the properties instead.

[mealViewController setLblNameString:model.Name];
[mealViewController setTxtDescString:model.Description];

Then, in your newWillAppear, setting them

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    lblName.text = lblNameString;
    txtDesc.text = txtDescString;
}

(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.

噩梦成真你也成魔 2024-12-30 18:50:07

我要做的是将 *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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文