Abdul Baais
Abdul Baais

Reputation: 13

Open ViewController on button click

I am new to iOS, can any one please help me to open and activity on Button click.

I have tried below methods, but the app remains on same ViewController, i am using Singleview Application type

 Second *sec = [[Second alloc] init];
 [self.navigationController pushViewController:sec animated:YES];

 Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondV"];
 [self.navigationController pushViewController:sec animated:YES];

can anyone please help me to solve this.

Upvotes: 0

Views: 1434

Answers (3)

anneblue
anneblue

Reputation: 706

to push to another view controller you need to set a UINavigationController in front of your UIViewController in storyboard. Your self.navigationController might be nil, that's why it is not pushing your view controller.

Therefore drag and drop a Navigation Controller to your storyboard and delete the default TableViewController (make sure to click on empty space in storyboard before selecting and deleting it) and connect the UINavigationController to your own 'single view controller' by right-clicking on the yellow arrow and connecting the rootViewController outlet with it).

Then, select the UINavigationController on your storyboard and in Attributes Inspector (the fourth symbol in the right panel) under the section View Controller select Is Initial View Controller.

Upvotes: 0

Sam
Sam

Reputation: 198

if you are using storyboard then Embed your viewcontroller to UINavigationController Editor->EmbedIn->NavigationController try this

Upvotes: 4

Anon
Anon

Reputation: 623

If not using Storyboard, then following might help:

Second *sec = [[Second alloc] initWithNibName:@"Second" bundle:nil];
[self.navigationController pushViewController:sec animated:YES];

For Storyboard enabled code:

Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:@"YourIdentifierForVC"];
[self.navigationController pushViewController:sec animated:YES];

BTW, I strongly suggest you to google the problem for solution & search SatckOverflow - first, before posting as a new question.

Also, you should reconsider the naming for the class. Just a suggestion.

Upvotes: 3

Related Questions