1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

missing keyword?

Discussion in 'Mac Programming' started by kevinrichardsuk, Jun 17, 2008.

  1. macrumors member

    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?

  2. macrumors 6502

    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];
  3. macrumors member

    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..
  4. macrumors 6502

    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.
  5. macrumors member

    Nutter - thanks, you've been very helpful.

Share This Page