비슷한 문제가 발생하여 두 작업을 모두 수행하기 위해 동일한 UIViewController
을 사용하게되었습니다. 두 유스 케이스는 근본적으로 동일합니다. 사용자가 정보를 입력 한 다음 저장합니다. 데이터의 유효성 검사는 동일하며 저장은 동일하며 프레젠테이션은 동일합니다. 모델 객체가 이미 존재하는지 여부 만 달라집니다. 다음의 예를 생각해
.H 파일 :
@interface WHItemViewController : UITableViewController
// This is the only attribute we really need to expose in our header
// so that the presenting view controller can set the model object when editing
@property (nontatomic, retain) WHItem* item;
@end
하는 .m 파일 :
@interface WHItemViewController()
// Other 'private' properties and methods
- (void)didSelectSaveButton:(id)sender;
@end
@implementation WHItemViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (self.item) { // setup the UI with the model object, if present.
self.fooText.text = self.item.foo;
self.barText.text = self.item.bar;
self.bazText.text = self.item.baz;
}
}
- (void)didSelectSaveButton:(id)sender {
if (!self.item) {
WHItem* item = nil;
// create a new instance from your managed object context
// and set it to the item property
self.item = item;
}
self.item.foo = self.fooText.text;
self.item.bar = self.barText.text;
self.item.baz = self.bazText.text
NSError* error = nil;
[WHDataAccess sharedInstance].managedObjectContext save:&error];
// Error handling, etc.
}
// Other methods
@end
이보기 컨트롤러의 유일한 목적은 편집을위한 프리젠 테이션 및 저장 모델 객체 데이터를 생성하는 것입니다.
나는 nib 파일을 공유하고 두 개의 별도의 'UIViewController' 하위 클래스를 만들 것입니다. – mAu
나는 둘 다 편집 작업으로 취급하고있다. 하나의 경우에는 편집되는 정보가 기본값 인 빈번한 빈 문자열 일뿐입니다. –