的UILabel控制在另一个视图控制器
答
你应该把文本模型中的类(假设你有一个,如果你不这样做,你需要创建它)。当最终用户编辑文本字段时,您的代码应该更改模型中的字符串;当标签显示时,您应该从模型中读取文本。在多个课程中共享您的模型的最简单方法是定义一个singleton。
部首:
@interface Model : NSObject
@property (NSString*) labelText;
+(Model*)instance;
@end
实现:
@implementation Model
@synthesize labelText;
+(Model*)instance{
static Model *inst;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
inst = [[Model alloc] init];
});
return inst;
}
-(id)init {
if (self = [super init]) {
labelText = @"Initial Text";
}
return self;
}
@end
使用模型:
// Setting the field in the model, presumably in textFieldDidEndEditing:
// of your UITextField delegate
[Model instance].labelText = textField.text;
// Getting the field from the model, presumably in viewWillAppear
myLabel.text = [Model instance].labelText;
是模型类的第一个视图控制器的一个子类? – Amelia777 2012-08-01 18:32:36
@ user1497323没有,模型类是应用程序的状态的持有人,如[模型 - 视图 - 控制器(https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals /CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW1)。看看编辑的例子。 – dasblinkenlight 2012-08-01 18:36:42
谢谢你的例子,你可能使用不同版本的Xcode?我有一个错误,说它无法识别“型号”类型。你知道另一种声明或称呼模型的方法吗? – Amelia777 2012-08-01 18:44:09