0

I am trying to navigate some view controller programatically. I have already add the UINavigationControllerDelegate in the header file...But the view didnt push out when I click the button..Can anyone tell did I do anything wrongly???

    -(void)nextPage
    {
        NSLog(@"1234");
        infoViewController* info = [[infoViewController alloc] init];
        [self.navigationController pushViewController:info animated:YES];
        info = nil;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.navigationController.delegate = self;
        startFutureView = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 320, 548)];
        startFutureView.backgroundColor = [UIColor redColor];
        [self.view addSubview:startFutureView];


        startPastView = [[UIView alloc] initWithFrame:CGRectMake(-160, 0, 320, 548)];
        startPastView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:startPastView];

        startInfoBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        startInfoBtn.frame = CGRectMake(80, 137, 180, 180);
        [startInfoBtn addTarget:self action:@selector(nextPage)                         forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:startInfoBtn];

// Do any additional setup after loading the view, typically from a nib.
    }
HebitzLau
  • 139
  • 1
  • 7
  • change the nslog to log self.navigationController. is it nil? – wattson12 Apr 27 '13 at 17:54
  • 1
    just adding the navcontrollerdelegate to the header file is not enough. Have you created an instance of the navigationController? is it assigned to the window's rootViewController? If your viewController where you are trying to push, is not a rootviewcontroller of a UINavigationController, nothing will happen. Adding more code about your AppDelegate and ViewController will help – Nitin Alabur Apr 27 '13 at 17:59

1 Answers1

1

There is no need to conform to UINavigationControllerDelegate to push a view controller. I'm guessing that your navigationController is nil.

The reason for this is that you are using a plain view controller, but what you need to do is create the view controller, and then wrap it in a navigation controller. As an example, assume you are now presenting viewController modally:

UIViewController *viewController = [[UIViewController alloc] init];

[self presentViewController:viewController animated:YES completion:nil];

doing this means viewController has no navigation controller, so you need to do this instead:

UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

[self presentViewController:navigationController animated:YES completion:nil];
wattson12
  • 11,176
  • 2
  • 32
  • 34