开发者

Access a UITableView from child class

I have two view controllers, class A and class B. Class B is a subclass of class A. Class B has a UITableView defined, and in the parent class A, I want to be able to [table reloadData]. So I want to access a member variable of class Bfrom parent class A. I tried doing the whole forward declaration thing but that didn't work out. In class B, I put @class A. And then in my class A, I tried declaring a class B object to access the tab开发者_开发知识库le like this: B *bView=[B alloc] init]; [bView.table reloadData] //doesn't work, I get error: Receiver B is a forward class and corresponding @interface may not exist

So not sure why that's not working, but maybe I shouldnt even be doing it like this. Since its a parent class, is there a better way to access child member variables?


The parent gets no implicit access to its childs member variables. So from A's point of view B is any old object.

If B is a subclass of A then you should be doing this in B.h

#import "A.h"

@interface B:A
{
...
}
@property (readwrite,retain) UITableView *table;

And if you wish to use B in A then you can do this in "A.h" in case where you want a B member

@class B

@interface A:Foo
{
B* bar;
}

Or don't bother with the forward declaration if you don't want a B member

In both cases in A.m

#import "B.h"

You can also use #include guards to stop double imports.

#ifndef AHEADER_H
#define AHEADER_H

@interface A:Foo
...
@end

#endif

No matter how many times you use #import A.h it will only get imported once.


You should not need to do this. Why not just reload the table in the subclass implementation? You can override Class A's methods in Class B if you want to add some new behaviors to existing code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜