views:

1113

answers:

3

Hello,

I haven't figured out that yet: I have a mainViewController that switches two views, viewControllerA and ViewControllerB. The way I switch the view is by having a UIButton (mainButton) in the mainViewController, and clicking on it switches viewControllerA <--> ViewControllerB.

Now here is my problem. My ViewControllerA has a UIButton (ButtonA). And I want that by clicking on it, it tells the mainViewController to switch to the other view (viewControllerB)

In other words, the child view (viewControllerA) should send a message to the mainViewController(its parent view) that it wants to fire a method that belongs to the main view, not to itself (viewA).

How could I achieve that please?

A: 

There are a few ways to achieve this: Take a look at protocols http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProtocols.html here, also take a look at the RootViewController use in some of apples sample project, Metronome here http://developer.apple.com/iphone/library/samplecode/Metronome/ is using this to switch from the main view to the preferences view. Look at modal view controllers and their interactions in the View COntroller programing guide, http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/PresentingModelViewControllers/PresentingModelViewControllers.html and you can also look at the answers here http://stackoverflow.com/questions/1124859/switch-between-3-or-more-views/1129182#1129182

Daniel
very good as well.
Yohann T.
+5  A: 

When communication to parent objects you have a few design patterns to choose from. Delegation and Notification are both good choices.

The big idea here is communication with loose coupling. Notifications use a Singleton to handle communication while Delegation uses weak references to parent objects. (Check out Cocoa With Love: retain cycles)

If you go with delegation, you can create an informal protocol for your ViewControllerA which MainViewController must conform to.

You may call it the ViewControllerADelegate protocol:

    @protocol ViewControllerADelegate

    @optional
    - (void)bringSubViewControllerToFront:(UIViewController*)aController;

    @end

Or ViewControllerA can post a notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyFunkyViewSwitcherooNotification" object:self];

And MainViewController should be listenting if it wants to know:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(swapThoseViews:) name:@"MyFunkyViewSwitcherooNotification" object:nil];
Corey Floyd
I like the notification concept, sounds like firing/catching events in .Net, I'm going to try that when I get home.Thx Corey!
Yohann T.
Works. (little typo: remove ":" in the @selector(swapThoseViews:) ).
Yohann T.
Why can't Apple's own documentation be so plain?
Wayne Hartman
Great stuff, I agree: Why can't Apple's own documentation be so plain.
maralbjo
Is it necessary to make a protocol, or just good practice?
maralbjo
if you don't define the protocol, you have to import the class header, which couples the classes. Defining a protocol alleviates this.
Corey Floyd
A: 

Yohann, you are wrong, the syntax @selector(swapThoseViews:) needs the ":" , must have had a different problem

Bogdan
you are correct.
Yohann T.