Accessing UIWidgets outside a ViewController

Discussion in 'iOS Programming' started by AlanMc, Nov 14, 2012.

  1. AlanMc macrumors newbie

    Joined:
    Nov 14, 2012
    #1
    Hello!

    What I basically want to do, is to set the label of a UITextView outside of a ViewController. Is that possible?

    What I have tried is the following:
    Code:
    @interface myViewController : UIViewController {
        ...
    }
    
    @property (nonatomic, retain) IBOutlet UITextView *outputTextField;
    
    @end
    and the synthesize code in the .m file.

    Then I tried to import myViewController.h in my other .m file and tried to access the TextView like this:
    Code:
    outputTextField.text = @"test";
    This however won't work. What do I need to do differently?

    Thank you,
    Alan
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What is the value of outputTextField when this line executes?:
    Code:
    outputTextField.text = @"test";
     
  3. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #3
    When you say "won't work", what happens exactly? Doesn't do anything? Or doesn't compile? If the latter, what's the error message?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Accessing views of one view controller by another view controller is usually a bad idea. If you do it at the wrong time the view won't exist so nothing happens.

    Also, this is a violation of MVC design. You should be updating the data model of the view controller and the second view controller should update its view, at the right time.
     

Share This Page