简体   繁体   English

使用XIB的视图作为子视图并访问其textfield属性

[英]Using a XIB's view as subview and access its textfield property

I'm trying to use NameSubView from NameSubViewController as a subview in MainViewController . 我正在尝试使用NameSubViewNameSubViewController作为MainViewController的子视图。 I made that work, but I also want to access the UITextField of the subview, something like this: 我做了那个工作,但我也想访问子视图的UITextField ,如下所示:

MainViewController.m : MainViewController.m

#import "NameSubViewController.h"

...

UIViewController *nameController = [[NameSubViewController alloc] initWithNibName:@"NameSubViewController" bundle:nil];
nameSubView = [nameController view];
[self.view addSubview:nameSubView];

NSString *textFieldString = nameSubView.textField.text;

But I'm having trouble making it work. 但我无法使其正常工作。 This is what I've did to create the subview: 这就是我创建子视图时所做的:

  • Made NameSubViewController class with XIB for interface 使用XIB制作NameSubViewController类用于接口

  • Made NameSubView class for the IB view, set the view to this class Identity Inspector 为IB视图NameSubView类,将视图设置为此类Identity Inspector

  • Created @property (nonatomic, strong) IBOutlet UITextField *textField; 创建@property (nonatomic, strong) IBOutlet UITextField *textField; in NameSubView and connected to the text field in IB. NameSubView并连接到IB中的文本字段。


Then I can load the subview, but I could not access the textField from MainViewController . 然后我可以加载子视图,但我无法从MainViewController访问textField。 So to get access to it I did change the Files Owner's class from NameSubViewController to NameSubView in Identity Inspector. 因此,为了访问它,我确实将NameSubView的Files Owner的类 NameSubViewController为Identity Inspector中的NameSubView

Now the NameSubViewController is accessed, but no property for the view is found, so the app crashes. 现在访问NameSubViewController ,但没有找到视图的属性,因此应用程序崩溃。 I don't know if I'm following the right procedure, can I now create a property for the NameSubView in NameSubViewController ? 我不知道如果我下面的正确的程序,我现在可以创建的属性NameSubViewNameSubViewController Or should I follow another procedure? 或者我应该遵循另一个程序?

  • Return the NameSubViewController to the file's owner in identity inspector. NameSubViewController返回到身份检查器中的文件所有者。
  • Add the NameSubView to the view in identity inspector. NameSubView添加到身份检查器中的视图。

Then use this code: 然后使用此代码:

#import "NameSubViewController.h"
// If view's declaration is located in a separate file then the next line is important
#import "NameSubView.h" 

...

// It is better to declare the view controller as 'NameSubViewController'
NameSubViewController *nameController = [[NameSubViewController alloc] initWithNibName:@"NameSubViewController" bundle:nil];
// A casting to 'NameSubView' should do the main magic
NameSubView *nameSubView = (NameSubView *)[nameController view];
[self.view addSubview:nameSubView];

// It is better to ensure that the view is of a correct class 
// before using its specific properties
if ([nameSubView isKindOfClass:[NameSubView class]]) {
  // Now it should work 
  // (assuming that you have a property 'textField' in 'NameSubView')
  NSString *textFieldString = nameSubView.textField.text;
}

First, change file's owner back to NameSubViewController . 首先,将文件的所有者更改回NameSubViewController Move the textfield IBOutlet declaration to NameSubViewController, and make sure everything is connected properly in the nib. 将文本字段IBOutlet声明移动到NameSubViewController,并确保在nib中正确连接所有内容。 Finally, declare your nameController variable to be of type NameSubViewController and not UIViewController. 最后,将nameController变量声明为NameSubViewController而不是UIViewController。 You should then be able to get the textfield with nameController.textField 然后,您应该能够使用nameController.textField获取文本nameController.textField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM