UIContainerView 中的 UITextField 表单 Returns nil 当文本存在时
UITextField Form inside UIContainerView Returns nil When Text Exists
层次结构:NavigationController(根)--> SignupViewController(第二级)--> SignupTableView(第三级)。 SignupViewController 包含一个嵌入 SignupTableView 的 ContainerView。 SignupTableView 包含您在图像中看到的字段。
我定义
@property (nonatomic,strong) SignupTableViewController *tableViewController and
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"signupFieldSegue"]){
SignupTableViewController *destination = (SignupTableViewController *)segue.destinationViewController;
self.tableViewController = destination;
}
}
SingupTableView 内部。
在 UIContainerView
中按下 IBAction 按钮验证 UITableView
中的字段,但是 return 从 UITextField
中编辑的值 SignupTableViewController
为零。我尝试了 self.tableViewController.usernameField.text
并定义了
- (NSString *)getUsername {
return _usernameField.text;
}
两种解决方案return nil 当文本确实存在时。有什么方法可以访问文本?
根据您在问题中所写的内容,您的容器视图似乎与主(父)视图控制器共享屏幕。无论如何,您首先需要确保在释放包含表单的视图控制器之前执行验证方法。
在您的父视图控制器中,您应该保留对 segue 的目标视图控制器的引用。这样,来自父视图控制器的验证可以轻松地从容器视图中的视图控制器检索数据。
首先,在你的父视图控制器中声明一个属性:
@property (nonatomic, weak) FormViewController *formViewController;
然后,在 prepareForSegue:sender:
中,将 属性 设置为 segue 的目标视图控制器。
- (void)prepareForSegue:(nonnull UIStoryboardSegue *)segue sender:(nullable id)sender {
if ([segue.identifier isEqualToString:@"signupFieldSegue"]) {
FormViewController *destination = (FormViewController *)segue.destinationViewController;
self.formViewController = destination;
// continue preparing for the segue...
}
}
然后,在您的验证方法中,使用 属性 访问 UITextField
值。这是一个使用 getUsername
方法的示例,假设 FormViewController
有一个 usernameField
属性:
- (NSString *)getUsername {
return self.formViewController.usernameField.text;
}
同样,请记住,如果您的表单视图控制器在您尝试访问其文本字段之前被释放,则其中的 none 将起作用。
层次结构:NavigationController(根)--> SignupViewController(第二级)--> SignupTableView(第三级)。 SignupViewController 包含一个嵌入 SignupTableView 的 ContainerView。 SignupTableView 包含您在图像中看到的字段。
我定义
@property (nonatomic,strong) SignupTableViewController *tableViewController and
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"signupFieldSegue"]){
SignupTableViewController *destination = (SignupTableViewController *)segue.destinationViewController;
self.tableViewController = destination;
}
}
SingupTableView 内部。
在 UIContainerView
中按下 IBAction 按钮验证 UITableView
中的字段,但是 return 从 UITextField
中编辑的值 SignupTableViewController
为零。我尝试了 self.tableViewController.usernameField.text
并定义了
- (NSString *)getUsername {
return _usernameField.text;
}
两种解决方案return nil 当文本确实存在时。有什么方法可以访问文本?
根据您在问题中所写的内容,您的容器视图似乎与主(父)视图控制器共享屏幕。无论如何,您首先需要确保在释放包含表单的视图控制器之前执行验证方法。
在您的父视图控制器中,您应该保留对 segue 的目标视图控制器的引用。这样,来自父视图控制器的验证可以轻松地从容器视图中的视图控制器检索数据。
首先,在你的父视图控制器中声明一个属性:
@property (nonatomic, weak) FormViewController *formViewController;
然后,在 prepareForSegue:sender:
中,将 属性 设置为 segue 的目标视图控制器。
- (void)prepareForSegue:(nonnull UIStoryboardSegue *)segue sender:(nullable id)sender {
if ([segue.identifier isEqualToString:@"signupFieldSegue"]) {
FormViewController *destination = (FormViewController *)segue.destinationViewController;
self.formViewController = destination;
// continue preparing for the segue...
}
}
然后,在您的验证方法中,使用 属性 访问 UITextField
值。这是一个使用 getUsername
方法的示例,假设 FormViewController
有一个 usernameField
属性:
- (NSString *)getUsername {
return self.formViewController.usernameField.text;
}
同样,请记住,如果您的表单视图控制器在您尝试访问其文本字段之前被释放,则其中的 none 将起作用。