本文介绍了UIStoryboard 容器将 IBOutlet 附加到父 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个 UITableView,它包含一个带有 UITextField 的静态单元格,它附在我的 LoginViewController 上.我在与故事板中的 UITextFields 相对应的头文件中添加了 IBOutlets.不幸的是,Xcode 不允许 UITableView 中的静态内容,您需要使用 UITableViewController.

I wanted to use a UITableView which contained static cells with a UITextField inside attached to my LoginViewController. I have added IBOutlets in the header file that correspond to the UITextFields in the storyboard. Unfortunately Xcode does not allow static content inside a UITableView you need to use a UITableViewController.

因此,我从故事板中分离出 UITableView 并使用 ContainerView 嵌入一个运行良好的 UITableViewController.附上设置截图.

Therefore I split out the UITableView from the storyboard and used a ContainerView to embed a UITableViewController which works well. Attached a screenshot of the setup.

我现在面临的问题是我无法将 UITextFields 链接到我的 LoginViewController,您可以从我的屏幕截图中看到我添加了一个 ObjectUITableViewController 我已经设置为我的 LoginViewController 并设法将 UITextFields 链接到它.但是当主视图加载时,它说我的 UITextFieldsnil

The problem I am now facing is I cannot link the UITextFields to my LoginViewController, you can see from my screenshot that I have added an Object to the UITableViewController which I have set to my LoginViewController and managed to link the UITextFields to it. But when the main view loads it says my UITextFields are nil

如何将我的 UITextFields 链接到我的 LoginViewController 中的 IBOutlets?

How can I link my UITextFields to my IBOutlets in my LoginViewController?

如果我必须对 UITableViewController 进行子类化,似乎我应该使用原型单元格...还有其他建议吗?

If I have to subclass the UITableViewController it seems like I should have just used a prototype cell... Any other suggestions?

推荐答案

1.) 将嵌入 segue 命名为TextFields"

1.) Name your embed segue to "TextFields"

2.) 如果你还没有,子类 UITableViewController 并将你的表视图控制器设置为故事板中的这个类.我们称之为TextFieldsTableViewController.

2.) If you haven't already, subclass UITableViewController and set your table view controller to this class in the storyboard. Let's call it TextFieldsTableViewController.

3.) 同样,如果您还没有将 IBOutlet 的文本字段添加到 TextFieldsTableViewController 的公共接口中:

3.) Again, if you haven't already, add IBOutlets for your text fields into TextFieldsTableViewController's public interface:

@property (weak, nonatomic) IBOutlet UITextField *firstNameField;
@property (weak, nonatomic) IBOutlet UITextField *usernameField;
@property (weak, nonatomic) IBOutlet UITextField *emailField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;

4.) 将文本字段的属性添加到 LoginViewController 的公共界面中(请注意,与上述不同的是,这些属性并不弱(它们也不是 IBOutlets,但这并不重要)):

4.) Add properties for your text fields into your LoginViewController's public interface (note that unlike the above, these are not weak (they are also not IBOutlets, but that doesn't really matter)):

@property (nonatomic) UITextField *firstNameField;
@property (nonatomic) UITextField *usernameField;
@property (nonatomic) UITextField *emailField;
@property (nonatomic) UITextField *passwordField;

5.) 在 LoginViewControllerprepareForSegue:sender: 方法中,将 TextFieldsTableViewController 的文本字段映射到 self 的文本字段在强制表视图控制器的视图立即加载后:

5.) In LoginViewController's prepareForSegue:sender: method, map the TextFieldsTableViewController's text fields to self's text fields after forcing the table view controller's view to load immediately:

if ([segue.identifier isEqualToString:@"TextFields"]) {

    TextFieldsTableViewController *textFieldsVC = segue.destinationViewController;

    // IMPORTANT: This forces the view to load and the outlets to get set.
    // If you don't do this, textFieldsVC's IBOutlets will all be nil at this point.
    // This is slightly inelegant in my opinion, but it works. A better solution might
    // be to set the text fields in TextFieldsTableViewController's viewDidLoad method.
    [textFieldsVC view];

    self.firstNameField = textFieldsVC.firstNameField;
    self.usernameField = textFieldsVC.usernameField;
    self.emailField = textFieldsVC.emailField;
    self.passwordField = textFieldsVC.passwordField;
}

6.) 您现在应该能够访问 LoginViewControllerviewDidLoad 方法中的文本字段.

6.) You should now be able to access the text fields in LoginViewController's viewDidLoad method.

这篇关于UIStoryboard 容器将 IBOutlet 附加到父 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:54