2014-05-22 3 views
1

chlidViewController 메서드를 사용하는 방법이 많이 있습니다. 그러나 childViewController에서 uitextfield 및 uiswitch의 값을 변경하고 설정하는 방법을 찾지 못했습니다.parentViewController의 childViewController에서 UITextField 및 UISwitch 값을 변경하는 방법

ChildViewController.h :

@protocol VVInformationTableViewControllerDelegate; 

@interface VVInformationTableViewController : UITableViewController 

@property (weak, nonatomic) id<VVInformationTableViewControllerDelegate> delegate; 

@property (weak, nonatomic) IBOutlet UITextField *nameTextField; 

@property (weak, nonatomic) IBOutlet UITextField *surnameTextField; 

@property (weak, nonatomic) IBOutlet UITextField *emailTextField; 

@property (weak, nonatomic) IBOutlet UITextField *locationTextField; 

@property (weak, nonatomic) IBOutlet UITextField *headlineTextField; 

@property (weak, nonatomic) IBOutlet UITextField *positionTextField; 

@property (weak, nonatomic) IBOutlet UITextField *companyTextField; 

@property (weak, nonatomic) IBOutlet UISwitch *messagesEnable; 

@end 

ParentViewControler.m : 도움을

- (void)viewDidLoad 
{ 
    self.currentAttendee = [VVAPIClient sharedClient].currentUser; 

    NSParameterAssert(self.currentAttendee); 

    [super viewDidLoad]; 
    [self.navigationController setNavigationBarHidden:YES]; 

    self.infoTableController = [[VVInformationTableViewController alloc] initWithNibName:@"InformationTableViewController" bundle:nil]; 
    [self addChildViewController:self.infoTableController]; 

} 

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    self.infoTableController.nameTextField.text = self.currentAttendee.firstName?:@""; 
    self.infoTableController.surnameTextField.text = self.currentAttendee.lastName?:@""; 
    self.infoTableController.emailTextField.text = self.currentAttendee.email?:@""; 
    self.infoTableController.locationTextField.text = self.currentAttendee.location?:@""; 

    self.infoTableController.headlineTextField.text = self.currentAttendee.headline?:@""; 
    self.infoTableController.positionTextField.text = self.currentAttendee.position?:@""; 
    self.infoTableController.companyTextField.text = self.currentAttendee.company?:@""; 

} 

-(void)viewDidLayoutSubviews{ 

    self.infoTableController.messagesEnable.on = NO; 
    self.infoTableController.nameTextField.tag = 0; 
    self.infoTableController.surnameTextField.tag = 1; 
    self.infoTableController.emailTextField.tag = 2; 
    self.infoTableController.locationTextField.tag = 3; 

    self.infoTableController.headlineTextField.tag = 5; 
    self.infoTableController.positionTextField.tag = 6; 
    self.infoTableController.companyTextField.tag = 7; 
} 

감사합니다.

+0

일반적으로 말해서 하나의보기 컨트롤러가 다른보기 컨트롤러에서 속성을 변경하는 것을 다시 고려해야합니다. 원하는 객체 간 분리 기능을 위반합니다. 이 경우'currentAttendee'를 속성으로'VVInformationTableViewController'에 전달하는 것이 좋을 것입니다. 그리고 자식에게 자신의 일을시키는 것이 좋습니다. –

답변

1

데이빗이 자신의 의견에서 말했듯이하지 말라.

다른보기 컨트롤러의 캡슐화에 위배되며 스파게티 코드로 연결됩니다.

다른 VC (보기 컨트롤러)보기를 비공개로 취급해야합니다.

표시해야하는 문자열 및 기타 상태 데이터를 보관하기 위해 하위보기 컨트롤러에 속성을 추가해야합니다. 그런 다음 하위보기 컨트롤러의 viewWillAppear 메서드에서 설정을 가져 와서 뷰 계층 구조에 적용 할 수 있습니다.

"currentAttendee"에 대한 많은 정보를 표시하고 있기 때문에 (모델 객체라고 생각합니다) 전체 참석자 객체에 대한 포인터를 전달하려고 생각할 수 있습니다. 자식을 표시하고 정보 자체를 표시하도록합니다.

자식의 개체를 편집 할 수 있고 복사본을 전달하고 자식에서 변경 한 내용을 커밋하거나 변경 내용을 취소하려는 경우 대리 메서드를 사용할 수 있습니다.

+0

두 컨트롤러에서 currentAttendee에 대한 정보를 얻을 수 있지만 parentViewController에서 키보드가 나타날 때 parentController에서 scrollView를 스크롤해야하기 때문에 textField를 사용할 때를 알아야합니다. – xav9211

+0

좋습니다. 부모 뷰 컨트롤러는 뷰를 관리하고 자식 뷰 컨트롤러는 ITS 뷰를 관리합니다. 그러나 부모 뷰 컨트롤러는 자식보기 컨트롤러에서 어떤 뷰가 선택되었는지 알 필요가 없습니다. –

+0

좋아, 내가 parrentViewController에서 uiTextFields의 대리인이 필요하지 않지만 내 키보드를 기각 모든 제스처 인식기를 얻을 필요가 있고 childViewController에서 scrollViewWillBeginDragging 메서드가 필요합니다 parrentViewController에서 호출되었습니다. 어떻게해야합니까? – xav9211