views:

70

answers:

2

Still having trouble with this language.

Ok, let's say I have two objects. The first is my application delegate, the second is a custom view containing the various buttons that make up the main menu. When a button is clicked, it is the menu that responds. However, I need to make use of certain instance variables in the application delegate (such as the Window) in order to implement the appropriate changes. In this case, I want the main menu to be removed and replaced with a new view. In other words, the main menu needs to trigger a method held in the application delegate.

So, how should I go about this?

+2  A: 

The preferred way is to create a delegate protocol for your view controller. Your application delegate can then implement this protocol and act on behalf of your view controller.

Check out the part about Delegation in the Cocoa Fundamentals Guide. Also read through the docs about Modal View Controllers, as this is quite similar to what you're trying to do. There are some code examples there as well.

ksoderstrom
Thanks for the information so far. I've already seen these documents, and suspected that protocols were the way to go. However I'm having real difficulty finding a good source on how to actually create custom protocols. For example, where should they be declared, How should they be declared, and how do you respond to them when you've got them? It's a real trial finding anything specific in the search function at the developer's guide through the sheer volume of material. Can anyone suggest a good tutorial for protocol creation?
Ash
The link in BP's answer describes the steps needed in detail.You can also check this blog post: http://jonsterling.github.com/2009/08/01/using-custom-delegates-in-objective-c.html
ksoderstrom
Ash: The Objective-C Programming Language document describes the syntax of protocols in detail. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProtocols.html You implement the methods the same way you implement any other methods; declaring that your class conforms to a protocol is simply a promise that you will implement all the methods that that protocol requires you to implement.
Peter Hosey
+4  A: 

I did a blog post on my web site a ways back that distills the process down into the simplest way I could come up with to describe setting up a delegate.

http://www.dosomethinghere.com/2009/07/18/setting-up-a-delegate-in-the-iphone-sdk/

BP
Brilliant! Thankyou BP, that blog post is exactly what I've been looking for! I really can't think why there isn't a tutorial of just that specific nature on Apple's own files, when it's such a fundamental part of custom object design.
Ash
No problem, some times I find that Apple's documentation can be a bit lengthy and hard to get through when you need something specific. Tweak the code, modify it, have fun with it, make it do something unusual and interesting. And you can always click the little check mark next to my answer... :D
BP