2012-08-01 5 views

답변

0

모델 클래스에 텍스트를 넣어야합니다 (모델 클래스가 있다고 가정하고, 그렇지 않은 경우 생성해야합니다). 최종 사용자가 텍스트 필드를 편집하면 코드가 모델의 문자열을 변경해야합니다. 라벨이 표시되면 모델에서 텍스트를 읽어야합니다. 여러 클래스간에 모델을 공유하는 가장 쉬운 방법은 singleton을 정의하는 것입니다.

헤더 :

@interface Model : NSObject 
@property (NSString*) labelText; 
+(Model*)instance; 
@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; 
+0

모델 클래스 제 뷰 컨트롤러의 하위 클래스이다

@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 

모델을 사용? – Amelia777

+0

@ user1497323 아니요, 모델 클래스는 [Model-View-Controller] (https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals)에서와 같이 애플리케이션 상태의 소유자입니다. /CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW1). 보기를 위해 편집을보십시오. – dasblinkenlight

+0

예를 들어, 아마도 Xcode의 다른 버전을 사용하고 있습니까? "모델"유형을 인식하지 못한다는 오류가 발생했습니다. 모델을 선언하거나 호출하는 또 다른 방법을 알고 있습니까? – Amelia777

관련 문제