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
NameSubViewControllerclass with XIB for interface -
Made
NameSubViewclass for the IB view, set the view to this class Identity Inspector -
Created
@property (nonatomic, strong) IBOutlet UITextField *textField;inNameSubViewand 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?
NameSubViewControllerto the file’s owner in identity inspector.NameSubViewto the view in identity inspector.Then use this code: