Niek H.
Niek H.

Reputation: 595

How do you correctly instantiate and show a view programmatically in a Single View Application for the iPad

I would like to know how to correctly show a view programmatically. I will try to explain what i did so far:

The application has a storyboard with two view controllers. One of them has an identifier: " detailview". When i click on a button on the initial view controller i want to show the view controller with the identifier "detailview". Currently i'm using this code:

TI4ViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"detailview"];
UIWindow* window = [UIApplication sharedApplication].keyWindow;                     
[window addSubview:vc.view];

This works fine to show the view "detailview". But when i add a button on the detailview controller and add a touchdown event in the TI4ViewController(the view controller that's connected to "detailview") i'm getting an exception while clicking on this button.

I'm getting the following exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType TestDown:]: unrecognized selector sent to instance 0x6e0f670'

I'm using Xcode Version 4.3.2 (4E2002)

I'm not sure what i'm doing wrong here. I think the ViewController is released, but i'm not sure.

Upvotes: 0

Views: 135

Answers (2)

Niek H.
Niek H.

Reputation: 595

Problem solved. I should have added the TI4ViewController as property of the initial view controller. Now it's removed from memory when you leave the function.

Upvotes: 0

zahreelay
zahreelay

Reputation: 1742

Instead of adding another view you can use segue. You can find an example here

Upvotes: 1

Related Questions