简体   繁体   中英

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 . I made that work, but I also want to access the UITextField of the subview, something like this:

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

  • Made NameSubView class for the IB view, set the view to this class Identity Inspector

  • Created @property (nonatomic, strong) IBOutlet UITextField *textField; in NameSubView and connected to the text field in IB.


Then I can load the subview, but I could not access the textField from MainViewController . So to get access to it I did change the Files Owner's class from NameSubViewController to NameSubView in Identity Inspector.

Now the NameSubViewController is accessed, but no property for the view is found, so the app crashes. I don't know if I'm following the right procedure, can I now create a property for the NameSubView in NameSubViewController ? Or should I follow another procedure?

  • Return the NameSubViewController to the file's owner in identity inspector.
  • Add the NameSubView to the view in identity inspector.

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 . Move the textfield IBOutlet declaration to NameSubViewController, and make sure everything is connected properly in the nib. Finally, declare your nameController variable to be of type NameSubViewController and not UIViewController. You should then be able to get the textfield with nameController.textField

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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