Access Objects from different classes

Discussion in 'iOS Programming' started by kieran12, Sep 17, 2008.

  1. kieran12 macrumors newbie

    Sep 17, 2008
    Hey guys,

    Please help me I've been trying to figure this out for days. I'm making an iPhone app and I'm nearly done. I have a UISwitch in one view and a UIButton in another view. The UIButton is declared in the RulesView.h file and the UISwitch is in the SettingView.h file. I want the SettingsView.h/m file to be able to access the button. I want the switch to make it hidden. Here is my code which should work fine:

    showAnswer.hidden = !solutionSlider.on;
    showAnswer is the name of the button and solutionSlider is the name of the switch.

    Basically I want to know how to access an object from a different view or class.

    (by the way I'm new to Mac development so try and explain everything to me if you can).

    Thanks for your help

  2. tacoman667 macrumors regular

    Mar 27, 2008
    You will need to pass the object as an argument into the new object through an init method. Create an initWithObject: and use that instead of init. Inside that new one you will do self = [super init]; as the first line then return self; at the end.
  3. kieran12 thread starter macrumors newbie

    Sep 17, 2008

    Thanks but im very new to iphone/mac development. I understand what init is but could you just explain a little more what I need to do and where i need to put it.

    thankyou so much



    I tried this at the start of my settingsView.m

    - (id)initWithObjects:(id)showAnswer{

    self = [super init];
    return self;

    but no luck.

    Am I doing it wrong?
  4. HiRez macrumors 603


    Jan 6, 2004
    Western US
    Basically you need to obtain a reference in the first object to the second somehow. You can mark the object you want to access as an IBOutlet and connect it in Interface Builder. For example:

    IBOutlet UIButton *showAnswer;
    IBOutlet UIButton *solutionSlider;
    and then:

    [appCon showAnswer].hidden = !([appCon solutionSlider].on);
    Probably you'll hold the reference outlet in a controller object, such as your application controller, and have interested objects ask the controller for the reference when they need it, although you could have those objects or views hold the reference themselves.

    Note that I haven't done much iPhone programming, but I think in this case the setup would be similar to a Cocoa desktop app.
  5. kieran12 thread starter macrumors newbie

    Sep 17, 2008
    no luck

    I get message appCon not declared. I should metion that the two IBOutlets are in separate classes.

    Another suggestion from a different forum was to pass objetc as an argument using initWithObjects:

    I'm not quite sure how to do this.

    Can anyone help


    Just understood what you meant by appCon lol sorry. I can't seem to get it to work. I'm surprised something that should be so simple and common has been such a problem. I've been trying to get an answer to this for days. hanks for your help though
  6. Sayer macrumors 6502a


    Jan 4, 2002
    Austin, TX
    You should have an application controller in your app. In this controller class create a get function to return the value of the control you want. It may be called app delegate.

    Basically you just do:

    + (BOOL) solutionSliderActive {
       return solutionSlider.on;
    If you use + (plus) instead of - (minus) at the beginning of the method name you can access the method from other objects, like where ever you want to know if the slider is on or not:

    showAnswer.hidden = [AppDelegate solutionSliderActive];
    Note that AppDelegate is the name of your app controller/delegate instance defined in the header file. If it was @instance AppController : NSObject you would use [AppController solutionSliderActive] instead.

    Easy, when you know how. Maybe not 100% PERFECT COCOA OMG!!!!111 but it works and you can always evolve the code later as you learn more sophisticated techniques. In the real world we have deadlines and limited man-hours to make it work or go look for a new tech job in the "down" economy.

Share This Page