【问题标题】:How to change value for UITextField and UISwitch from childViewController in parentViewController如何从 parentViewController 中的 childViewController 更改 UITextField 和 UISwitch 的值
【发布时间】:2014-05-22 22:52:57
【问题描述】:

我发现了很多如何使用chlidViewController的方法。但我找不到如何从 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;
}

感谢您的帮助。

【问题讨论】:

  • 一般来说,应该重新考虑让一个视图控制器更改另一个视图控制器的属性。它违反了对象之间所需的功能分离。在这种情况下,您最好将currentAttendee 作为属性传递给VVInformationTableViewController,让孩子自己做。

标签: ios objective-c uitableview childviewcontroller


【解决方案1】:

正如大卫在他的评论中所说,不要。

它违反了其他视图控制器的封装,导致意大利面条代码。

您应该将其他 VC(视图控制器)的视图视为私有视图。

您应该做的是向子视图控制器添加属性以保存您需要显示的字符串和其他状态数据。然后在您的子视图控制器的 viewWillAppear 方法中,您可以获取设置并将它们应用于您的视图层次结构。

在您的情况下,由于您正在做的是显示有关“currentAttendee”的一大堆信息(我猜这是一个模型对象),您可能需要考虑将指向整个参加者对象的指针传递给子,并让它自己显示信息。

或者,孩子可以编辑对象,您可能想要传递一个副本,并在您想要提交在孩子中所做的更改时使用委托方法,或者如果您想要放弃更改,则只需返回。

【讨论】:

  • 没关系。父视图控制器管理其视图,子视图控制器管理 ITS 视图。然而,父视图控制器不需要知道在子视图控制器中选择了哪个视图。
  • 好的,我不需要在 parrentViewController 中有 uiTextFields 的委托,但我需要获取所有关闭我的键盘的手势识别器,我需要在 parrentViewController 中调用 childViewController 的方法 scrollViewWillBeginDragging。我该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多