将一个类中声明的变量传递给另一个类

时间:2012-07-16 16:10:34

标签: objective-c ios xcode

我试图在一个视图中的文本字段中设置一个数字,该数字由一个类控制并使该数字出现在另一个由其他类控制的视图中的标签中,我该怎么办?

2 个答案:

答案 0 :(得分:1)

非常简单的方法是NSUserDefault。我不推荐这个,但这是一种获取数据的方法

保存

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString

[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger

[prefs setInteger:42 forKey:@"integerKey"];

// saving a Double

[prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float

[prefs setFloat:1.2345678 forKey:@"floatKey"];

// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];

Retrieving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString

NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger

NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float

float myFloat = [prefs floatForKey:@"floatKey"];

答案 1 :(得分:0)

多重转发,如:

How can I pass a parameter into a view in iOS?

iphone Pass String to another .m file in project

您还可以查看segueprotocol delegate