2

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

user2353519
  • 155
  • 2
  • 3
  • 9

4 Answers4

2

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];
}
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
  • ya thanks for help and it works ..can you tell me why we added UINavigationController in appDelegate?? – user2353519 May 06 '13 at 05:54
  • and if we need to press button and create a new stack without UINavigationController then what can we do?? – user2353519 May 06 '13 at 05:55
  • Page1 *button =[[Page1 alloc] initWithNibName:@"Page1" bundle:nil]; [self presentViewController:button animated:YES completion:nil]; – Ushan87 May 06 '13 at 06:01
  • If you don't want to use a navigation controller here, Without changing App Delegate, try this code for button press: -(IBAction)stack:(id)sender{ Page1 *button =[[Page1 alloc] initWithNibName:@"Page1" bundle:nil]; [self presentViewController:button animated:YES completion:nil]; } – Thilina Chamath Hewagama May 06 '13 at 06:02
  • ya thanks man !! it works but like others saying to add nib name ,..whats the difference if i make use of nib name in code?? – user2353519 May 06 '13 at 06:05
  • When we create an object of an existing viewcontroller (in your case Page1), when we initialise the controller, we have to provide the name of the .xib file you have used. I have done that part in my code. :) – Thilina Chamath Hewagama May 06 '13 at 06:09
  • ok If we dont give that nib name and directly initializes like i did in my code...does it make difference?> – user2353519 May 06 '13 at 06:15
  • ya simply i used Page1 *button =[[Page1 alloc]init]; and it also works – user2353519 May 06 '13 at 06:24
  • this will help you, http://stackoverflow.com/questions/2224077/when-should-i-initialize-a-view-controller-using-initwithnibname – Thilina Chamath Hewagama May 06 '13 at 06:29
0

Try this

-(void)aboutButtonPressed {
    AboutViewController *aboutView =[[AboutViewController alloc] initWithNibName:@"About" bundle:nil];
    [self.navigationController pushViewController:aboutView animated:YES];
    [aboutView release];
}
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

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;
Dhara
  • 4,093
  • 2
  • 36
  • 69
0

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

Dhara
  • 4,093
  • 2
  • 36
  • 69
Tom Sawyer
  • 596
  • 5
  • 17