Reputation: 155
I have two UIViewController
class.(with names:RootViewController & SecondViewController)
I add SecondViewController in FirstViewController with this code :
SecondViewController *secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self.view addSubview:secondView.view];
So I create one UITextField in SecondViewController with code:
@interface SecondViewController : UIViewController <UITextFieldDelegate>
@property (nonatomic,strong) UITextField *text1;
@end
@implementation SecondViewController
@synthesize text1;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.view.frame = CGRectMake(0, 200, 320, 300);
text1 = [[UITextField alloc] initWithFrame:CGRectMake(20,70,180, 32)];
text1.textAlignment = NSTextAlignmentRight;
text1.layer.borderWidth = 1.0f;
text1.layer.borderColor = [[UIColor grayColor] CGColor];
text1.autocorrectionType = UITextAutocorrectionTypeNo;
text1.delegate = self;
[self.view addSubview:text1];
}
- (IBAction)BackgroundTap:(id)sender {
[text1 resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[text1 resignFirstResponder];
return YES;
}
Now My Problem Here : when run app and click on my textfield app crashed!!! why??? when app crashed show my this code :
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
this is my error :
Upvotes: 0
Views: 645
Reputation: 1552
Your view controller containment is done incorrectly:
SecondViewController *secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController"
bundle:nil];
[self addChildViewController:secondView]; //Assuming self is view controller
[self.view addSubview:secondView.view];
[secondView didMoveToParentViewController:self];
More info here: http://www.objc.io/issue-1/containment-view-controller.html
Upvotes: 4