I have a class: MainViewController
An instance of that controller is controlling the current view.
I have another class: DetailClass in whi开发者_如何学编程ch I have an instance of MainViewController, myMainViewController.
How can I set myMainViewContoller equal to the instance of MainViewController currently controlling the view seen by the user?
I think you're confusing "instance" and "pointer to instance." Your question would make much more sense if you said that DetailClass has an instance variable that's a pointer to an instance of MainViewController. I'll assume that's what you meant.
Usually in these situations, one of the controllers has created the other one, or some other object has created both of them. The first case is common in navigation-based apps, while the second is likely if the two controllers are managed by a tab bar controller. Either way, there's generally some object that knows about both controllers. So, let's say that your MainViewController creates an instance of DetailClass. If that's the case, it can simply pass a pointer to itself as part of the initialization, or perhaps after the DetailClass instance has been created. Does DetailClass have a -setMyMainViewController: method? If yes, MainViewController might have some code that looks like this:
//...
DetailClass *detailController = [[DetailClass alloc] initWithNibName:nil bundle:nil];
[detailController setMyMainViewController:self];
//...
精彩评论