-2

如果我在一个班级中有一个标签并想更改它显示的文本,我怎么能从另一个班级获得这个?

4

2 回答 2

1

在 Objective-C 中,您可以properties有效地自动创建用于访问实例变量的 getter 和 setter。

@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

然后从您的其他类中,它是使用点运算符访问这些属性的简单案例。

myClassInstance.instanceLabel.text = @"newText";

您不必使用点运算符:

[myClassInstance instanceVariable].text = @"newText";
于 2012-08-27T17:52:10.573 回答
-1

通常为此使用设置功能。

IE。伪代码:

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");
    }
}

然后调用 SecondClass::performChange() 会将“YourClass myInstance's”实例变量更改为“hello”。

于 2012-08-27T17:48:03.190 回答