MTahir
MTahir

Reputation: 1171

Add and Navigate to a new view in Single Application

This question might have been answered, if yes, please share the link.

I have created a Single View Application, It works fine, but now I have added a new view and on a button click, wants the new view to appear.

This is the code for click action,

    SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:settingsViewController animated:YES];

The Default ViewController now looks like this in .h file

    @interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

The SettingsViewController.m has a default

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{}

Can I add another view to "Single View Application" like this or should I chose another template for my project ?

Thanks.

Upvotes: 1

Views: 1725

Answers (3)

MTahir
MTahir

Reputation: 1171

In iOS 5, switching between views works a bit different i think,

I have created a few apps with the above mentioned code for switching views.

But now, I have to write it like this to work:

    SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];
    [self presentModalViewController:settingsViewController animated:YES];

Upvotes: 0

Justin Paulson
Justin Paulson

Reputation: 4388

You need to create a UINavigationController in your AppDelegate. Then make your ViewController the rootViewController of the UINavigationController. Then you will be able to push and pop views.

Here is the code to create the rootViewController where mainNavigationController is the UINavigationController in your AppDelegate:

ViewController *vc = [[ViewController alloc] init];
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];

Once you have the ViewController set up as the rootViewController it will conform to the UINavigationController push and pop methods to create a stack of UIViewControllers.

Upvotes: 1

pdesantis
pdesantis

Reputation: 879

That is fine. The single view application template is just a barebones template. You can add any type of navigation you like to it.

Upvotes: 1

Related Questions