PDA

View Full Version : missing keyword?




kevinrichardsuk
Jun 17, 2008, 04:08 AM
I need to send an integer back to my AppDelegate instance, from a class instance created by it.

[AppDelegate setNumber:1];

This presumes the method in AppDelegate is +(void)setNumber and hence won't allow access to my instance variables. I understand I'm sending a message to AppDelegate and not an instance of it..

Am I missing a keyword (such as self) to access the instance of AppDelegate?

Thanks



Nutter
Jun 17, 2008, 05:28 AM
As I understand it, you're trying to send a message to the AppDelegate class object from an AppDelegate instance.

Your code above is fine, but I would consider the following best practise:

[[self class] setNumber:1];

kevinrichardsuk
Jun 17, 2008, 05:41 AM
As I understand it, you're trying to send a message to the AppDelegate class object from an AppDelegate instance.

Your code above is fine, but I would consider the following best practise:

[[self class] setNumber:1];

Thanks for the reply. I'll try and explain a bit better..

I've created an instance of a custom UIView (called TestView) from the AppDelegate instance. I've added a subview to that view, which can be pressed.

The press event triggers -(void)touchesBegan in my TestView instance, I need to pass [touch view] back to my AppDelegate instance for processing, but I don't know how to reference my instance of AppDelegate, only the class object..

Nutter
Jun 17, 2008, 06:03 AM
It sounds like you should be subclassing UIControl rather than UIView. UIControl has a target/action facility which allows controllers to register for callbacks messages (actions) when the user interacts with the control.

Otherwise, you could pass the AppDelegate instance to your view using a custom init method, and keep it around as an instance variable, but this is not good design, as it creates an unnecessary dependency between the view object and the controller.

kevinrichardsuk
Jun 17, 2008, 06:36 AM
Nutter - thanks, you've been very helpful.