-2

If I had a label in one class and wanted to change what text it displayed, how could I this from a different class?

user1628311
  • 166
  • 1
  • 3
  • 12
  • 1
    Show your code and give some more context then I am sure you will get the one or other response. – hol Aug 27 '12 at 17:47

2 Answers2

1

In objective-c you have properties for effectively automating the creation of getters and setters for accessing instance variables.

@interface MyClass
{
    UILabel *instanceLabel; //Not required, but I find it can make it clearer
}

@property (nonatomic, retain) UILabel *instanceLabel;

@end



@implementation MyClass

@synthesize instanceLabel; //Not required as of XCode 4.4
@end

Then from your other class its a simple case of using the dot operator to access those properties.

myClassInstance.instanceLabel.text = @"newText";

You don't have to use the dot operator:

[myClassInstance instanceVariable].text = @"newText";
James Webster
  • 31,873
  • 11
  • 70
  • 114
-1

Usually one's using set-functions for this.

ie. pseudocode:

class YourClass 
{
    private var str;

    public YourClass(var str)
    {
        this.str = str;
    }

    public setString(var str)
    {
        this.str = str;
    }

}




class SecondClass 
{
    private final YourClass myInstance;

    public SecondClass(final YourClass myInstance)
    {
        this.myInstance = myInstance;
    }

    public performChange()
    {
        myInstance.setString("hello");
    }
}

Calling SecondClass::performChange() would then change "YourClass myInstance's" instance variable to "hello".

phew
  • 808
  • 1
  • 15
  • 34