开发者

ObjC/iPad buttons in subviews calling methods in master controller. How?

Is my model all a mess?

I'm writing an iPad app with multiple views. I have a masterViewController that subviews other view controllers. Some of these subviews contain buttons.

The user clicks these buttons to change views. It is my intention that the masterViewController be responsible for changing these views. But how?

How can the masterViewCont开发者_Python百科roller tell when the button is pressed?

@implementation subViewControllerOne

- (IBAction) switchToSubViewTwo {

//Where to I send my message?
[masterViewController myMethod] //doesn't work because I need to message the specific instance

}


You could

  1. Pass a reference to your master controller to the sub controllers and tell it that way
  2. Make your master controller a singleton and reference it that way
  3. You could have your sub controllers post an NSNotification that the master listens for.


You might want to go by the AppDelegate to find a reference to your controller.

Make sure you add an outlet for your masterViewController in the AppDelegate and connect it (using Interface Builder). Then, in your subview's controller, get a reference to your AppDelegate and run your great method, like so:

  MyAppDelegate *delegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
  [delegate myMethod];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜