How can UIView access property of UIViewController?

Discussion in 'iOS Programming' started by cwm1021, Dec 22, 2010.

  1. cwm1021, Dec 22, 2010
    Last edited: Dec 22, 2010

    cwm1021 macrumors newbie

    Dec 22, 2010
    Hello Guys

    The program is basically like this in a nutshell:


    There is a UIViewController, it has an instance variable of NSInteger:

    I declare it in the UIViewController .h file as:

    Then, I synthesize it in its .m file:

    The view (UIView) of this UIViewController has a method doSomething: needs to access the value of theNum.

    Here is the code in this "view"'s .m file:


    So how can I get the value of theNum from UIViewController to its UIView?

    Thanks for advanced! And wish you guys Merry Xmas. :)
  2. cnstoll, Dec 22, 2010
    Last edited: Dec 23, 2010

    cnstoll macrumors 6502

    Aug 29, 2010
    It shouldn't work like that. Your "ViewController" should have a method that some how modifies "The View".

    Your "ViewController" can access it's property with:
    If your "view" needs a variable from your "ViewController" then you should pass that variable from the ViewController when you create the "view" object.

    View Controller Programming Guide

    A little more information about what you're trying to do would be helpful.
  3. flummoxed macrumors member

    Nov 27, 2010
    Yes. Your subclassed UIView definition cannot be aware of the existence to the viewController variable.

    You need to add an NSInteger instance variable and property to your UIView subclass.

    Then when you create the instance of the subclass in the viewController code
    you can pass the value between the properties.

    my_uiview.number = self.number;
    "self" being the viewController since you are implementing this code in the viewController.

    This has to be done outside your UIView instance method, before you call the method. The my_uiview.number variable is referenced inside your UIView method, but the value is passed between them outside.

    You could also pass the viewController number as an argument to the UIView method.
    - (void)doSomething:(NSInteger*) a_number{
     //do something with the number
    Then the call to the UIView method in the viewController would look like this...
    [my_uiview doSomething:self.number];
    I hope that is correct. I'm new to this as well.

Share This Page