user2353519
user2353519

Reputation: 155

PushViewController to navigate to next stack

I am new to IOS .what I am doing is I just simply placed a UIButton on my main view and I want when I click on that button i t should take me to the next stack . for that what I am doing is

-(IBAction)stack:(id)sender{
 Page1 *button =[[Page1 alloc]init];
 [self.navigationController pushViewController:button animated:YES];
}

I simply add this in my code ...but it is not working.

Do I need to put UINavigationController in main view or in AppDelegate to press button and create new stack with navigation

Upvotes: 2

Views: 4372

Answers (4)

Tom Sawyer
Tom Sawyer

Reputation: 596

Please do the following thing: In AppDelegate.h

@property (nonatomic, retain) UINavigationController *navigationController;

In AppDelegate.m

@implementation AppDelegate

@synthesize navigationController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

and when you want to push the next viewcontroller , you can write the above code :

-(void)fontButtonPress{
      FontViewController *fontViewController = [[FontViewController alloc]  initWithNibName:@"FontViewController_iPhone" bundle:nil];
  [self.navigationController pushViewController:fontViewController animated:YES];
}

Let Me know if You have any issue.

Thanks

Upvotes: 0

Dhara
Dhara

Reputation: 4089

Yes you need to write following lines in appdel

self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;

Upvotes: 0

Lithu T.V
Lithu T.V

Reputation: 20021

Try this

-(void)aboutButtonPressed {
    AboutViewController *aboutView =[[AboutViewController alloc] initWithNibName:@"About" bundle:nil];
    [self.navigationController pushViewController:aboutView animated:YES];
    [aboutView release];
}

Upvotes: 0

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

in AppDelegate,

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

and change your button press method,

-(IBAction)stack:(id)sender{
    Page1 *button =[[Page1 alloc] initWithNibName:@"Page1" bundle:nil];
    [self.navigationController pushViewController:button animated:YES];
}

Upvotes: 2

Related Questions