New to iOS dev. Kindly correct me if i am wrong?
I have a UIWindow, and rootviewcontroller is :
@interface ViewController : UIViewController
{
IBOutlet UIButton *but;
}
@property (nonatomic, strong) UIButton *but;
-(IBAction) butButtonPressed;
@end
ie: If i make this Viewcontroller as a root view controller, the UIView that is available in the ViewController is displayed. Understood.
I have created a new class inherited from UIViewController, and along with its .xib file.
So xib file name is : view1.xib,
My Objective is to display this view when the button is pressed.
Now i have created a button and button press invokes butButtonPressed. Inside of butButtonPressed, i did the following.
myViewController *vcontroler = [[ViewController alloc] initWithNibName:@"view1" bundle:nil];
[self.view.window addSubview:vcontroler.view];
Application crashes. What am i doing wrong ? Kindly point out.
This...
...is a really bad strategy (and I seriously wish I knew where people are finding it as a way of showing views). If you create a view controller, you should use it rather than just treating it as a temporary container that you can rip views out of.
If you want
vcontrolerto look like a child of your first controller (and you have aUINavigationController), usepushViewController:animated:. Otherwise you can show it as modal withpresentModalViewController:animated:.If you only want to add a view to the existing display, put the view in
ViewController's hierarchy and show/hide it.If you absolutely must have it as a sub-controller of
ViewControllerthen you need to keep a reference to it and manage its lifecycle inside the owning controller.