开发者

in UINavigationController how to call a parent's function

NSArray *allControllers = self.navigationController.viewControllers;
UITableViewController 开发者_开发知识库*parent = [allControllers lastObject];
[parent myFunction]; // not work

myFunction is a function in parent, but this code is not work, where is wrong


[self.navigationController.viewControllers lastObject] will return you last controller pushed on stack which is controller you are executing this code from. To get previous controller you can use [allControllers objectAtIndex:allControllers.count - 2] but this is a very bad idea:

1) Nobody can guarantee that you controller will not some day become a root controller. Then code above will crash your application because there are no controllers before it.

2) Nobody can guarantee that somebody will not push 'wrong' controller before yours.

So my advice - make property like @property (retain) ParentController *parentController; in your 'child' controller and set it explicitly to point to 'parent' controller.


Checking the documentation I can't see any reference to that method.

Can you post the error message?

A similar question has been already answered, take a look at the proposed solution.

Regards.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜