访问其他类的属性 [英] Accessing property of other class

查看:36
本文介绍了访问其他类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController,它有三个视图:Category -> Sub-Category -> Detail View.Sub-Category 和Detail View 的内容取决于在Category 视图中单击了哪一行.这是通过名为categoryClicked"的属性完成的,并在 sub-category.m 文件中声明.它的值在 category.m 文件中由以下代码行给出.

I've got a UIViewController which has three views: Category -> Sub-Category -> Detail View. The content of the Sub-Category and Detail View depends on which row is clicked in the Category view. This is done with a property called 'categoryClicked' and is declared in the sub-category.m file. It's value is given in the category.m file by the following lines of code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Sub-category *sub = [[Sub-category alloc] initWithNibName:@"Sub-category" bundle:nil];

    sub.categoryClicked = [_categoryArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:rubrieken animated:YES];

}

这很好用,直到我想在细节视图中使用 categoryClicked 的值.在 category.m 和 DetailView.m 中都导入了 sub-category.m.出于测试目的,我在 Detail View 上贴了一个标签,在 DetailView.m 文件中我有以下代码:

This works perfectly fine until I want to use the value of categoryClicked in the Detail View. In both the category.m and the DetailView.m sub-category.m is imported. For testing purposes I putted a label on the Detail View and in the DetailView.m file I've got the following code:

Sub-category *sub = [[Sub-category alloc] initWithNibName:@"Sub-category" bundle:nil];

label.text = sub.categoryClicked;

我确信这段代码应该可以完成这项工作,但实际上我得到了一个空标签.有没有人能告诉我我做错了什么.

I'm convinced this code should do the job but in fact I get an empty label. Is there anybody able to tell me what I'm doing wrong.

编辑
categoryClicked 是在 Sub-category.h 中声明并在 Sub-category.m 中合成的属性.想发布更多代码,但没有更多相关代码.

Edit
categoryClicked is a property declared in Sub-category.h and synthesized in Sub-category.m. Wanted to post some more code but there is no more relevant code.

推荐答案

当您alloc 一个对象时,您正在为其分配内存空间.然后当你使用带有 init 的东西时(比如 initWithNibName),你就是在初始化它.所以当你为 sub 分配内存空间并初始化它时,你就有了一个对象.但是随后您再次allocinit,这将创建一个全新(且完全不相关)的对象.确保移除任何可能破坏旧对象的东西.

When you alloc an object, you're allocating memory space for it. Then when you use something with init (like initWithNibName) you're initializing it. So when you allocate memory space for sub and initialize it, you have one object. But then you alloc and init again, which creates an entirely new (and completely unrelated) object. Make sure that you remove anything that could destroy your old object.

另外,* 符号意味着 sub 是一个指针(它指向一个内存位置).每当您使用赋值运算符 (=) 时,您都在告诉它指向一个新事物.您正在做的是告诉 label.text 指针指向 sub 所指向的内容.但是,如果您更改 sub 指向的内容并将 label.text 指向同一事物,则两者都没有指向您想要的值.

Also, the * symbol means that sub is a pointer (it points to a memory location). Whenever you use the assignment operator (=) you're telling it to point to a new thing. What you're doing is telling the label.text pointer to point at what sub is pointing at. But if you change what sub is pointing at and point label.text at the same thing, neither one is pointing at the value you want.

希望这足够清楚,如果它不是尝试发布更多代码,也许有人可以建议确切的更改.

Hope this is clear enough, if it's not trying posting some more code and maybe someone can suggest exact changes.

-编辑-

如果你想拥有一个对象的引用,你只能通过几种方式来获得它.重要的是要知道您不能真正创建"对现有对象的引用.您必须与对象有某种联系.

If you want to have a reference to an object you can only get it a few ways. What's important to know is that you can't really "create" a reference to an existing object. You have to have some connection to the object.

  • 使用类似 Category c = [[Category alloc] init]; 在文件中声明你想要引用的对象,记住这会创建一个新对象,它不会创建一个引用到现有对象.但是,在另一个对象内部创建对象意味着一个人拥有"另一个对象,并且可以用它做任何想做的事情(显然包括访问属性和调用方法).
  • 使用对象的链"来获取对您的对象的引用.因此,如果您的文件拥有一个拥有您想要的对象的文件,您可以使用 topfile.otherfile.objectyouwant.最明显的例子是获取对子视图拥有的对象的引用.
  • Declare the object inside of the file you want the reference in with something like Category c = [[Category alloc] init]; Remember this creates a new object, it won't create a reference to an existing object. However, creating the object inside of another object means that one "owns" the other and can do whatever it wants with it (obviously including accessing properties and calling methods).
  • Use a "chain" of objects to get a reference to your object. So if your file owns a file that owns the object you want you can use topfile.otherfile.objectyouwant. The most obvious example of this is getting a reference to an object owned by a subview.

这是最基本的;请记住,没有任何可以按名称调用的全局"对象.如果您的问题没有因此解决,请查看一些示例代码并尝试弄清楚引用是如何工作的,或者发布另一个与您的问题更密切相关的问题

That's about as basic as it gets; just remember that there aren't any "global" objects that you can just call by name. If your problem isn't solved by this, either look at some sample code and try to figure out how references work, or post another question that's more closely related to your problem

这篇关于访问其他类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆