FROM : Ken Thomases
DATE : Wed May 14 21:40:07 2008
On May 14, 2008, at 12:43 PM, James Churchman wrote:
> WebViewController *webViewController = [[WebViewController alloc]
> init];
>
>
> So now i have all my code running in my "webViewController" controller
> object, but i really need to call a method that is in the "
> MainViewController", which is in the the class "above",
> that actually initialized the "webViewController".
>
> (in fact i really want to message something in AppDelegate but
> allowing me
> to message the class "above" should allow me
> to extend this principle up future)
>
>
> How is this posible, can i pass an id of the "parent"
> (MainViewController) object
> to the webViewController that will allow it to pass messages back
> to it?
There's no "automatic" way for an object to receive a pointer to the
object which allocated it. You can pass it in explicitly. To
accomplish that, you would implement an alternative initializer
method which takes the pointer as a parameter. Something like:
- (id) initWithRelatedObject:(MyClass*)relatedObject
{
// ...
}
The internals of that initializer method would look mostly the same
as a typical initializer but would probably make use of relatedObject
somehow. You should read up on the whole topic of designated
initializers, though, to make sure you cover your bases properly.
You want to take care of the possibility that a client of your
WebViewController will try to use one of the initializers of the
superclass, and you want your new initializer to invoke the proper
initializer of the superclass.
That said, if you want to access the delegate of the main application
object, you can just use [NSApp delegate] to obtain it. That may be
a more appropriate design, depending on what you're actually trying
to achieve.
Cheers,
Ken
DATE : Wed May 14 21:40:07 2008
On May 14, 2008, at 12:43 PM, James Churchman wrote:
> WebViewController *webViewController = [[WebViewController alloc]
> init];
>
>
> So now i have all my code running in my "webViewController" controller
> object, but i really need to call a method that is in the "
> MainViewController", which is in the the class "above",
> that actually initialized the "webViewController".
>
> (in fact i really want to message something in AppDelegate but
> allowing me
> to message the class "above" should allow me
> to extend this principle up future)
>
>
> How is this posible, can i pass an id of the "parent"
> (MainViewController) object
> to the webViewController that will allow it to pass messages back
> to it?
There's no "automatic" way for an object to receive a pointer to the
object which allocated it. You can pass it in explicitly. To
accomplish that, you would implement an alternative initializer
method which takes the pointer as a parameter. Something like:
- (id) initWithRelatedObject:(MyClass*)relatedObject
{
// ...
}
The internals of that initializer method would look mostly the same
as a typical initializer but would probably make use of relatedObject
somehow. You should read up on the whole topic of designated
initializers, though, to make sure you cover your bases properly.
You want to take care of the possibility that a client of your
WebViewController will try to use one of the initializers of the
superclass, and you want your new initializer to invoke the proper
initializer of the superclass.
That said, if you want to access the delegate of the main application
object, you can just use [NSApp delegate] to obtain it. That may be
a more appropriate design, depending on what you're actually trying
to achieve.
Cheers,
Ken
| Related mails | Author | Date |
|---|---|---|
| James Churchman | May 14, 19:43 | |
| James Churchman | May 14, 19:45 | |
| Ken Thomases | May 14, 21:40 |






Cocoa mail archive

