Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push another View in UINavigationController?

How to push a view using a button in a navigation controller? I tried to follow this example: http://www.cimgf.com/2009/06/25/uitabbarcontroller-with-uinavigationcontroller-using-interface-builder/

This is exactly what I need, I have an UITabBarController with 4 tabs and one of them is an UINavigationController. I want to push that view containing the navigation controller from the first view- which is a simple UIView. It doesn't work, I tried also with a programmatically created button and nothing happens. Any pointers?

Here's the code I have

@interface HomeViewController : UIViewController {

}

-(IBAction)pushViewController:(id)sender;

@end

---

#import "HomeViewController.h"
#import "NewViewController.h"

@implementation HomeViewController

-(IBAction)pushViewController:(id)sender {
        NewViewController *controller = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
    [[self navigationController] pushViewController:controller animated:YES];
    [controller release], controller = nil;
}

Here is also the screenshot with the connections http://imageshack.us/photo/my-images/847/screenshot20110719at620.png/

I've tried what you guys suggested and still nothing, the buttons( whether they are created in Interface Builder or programmatically ) act like they're not linked to any method.

like image 606
ftwhere Avatar asked Dec 21 '22 11:12

ftwhere


2 Answers

Put this where you want to create the button: (e.g. the Root VC's -viewDidLoad)

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100.0, 100.0, 100.0, 40.0);
[button setTitle:@"Press" forState:UIControlStateNormal];
[viewController.view addSubview:button];

[button addTarget:self action:@selector(didPressButton:) forControlEvents:UIControlEventTouchUpInside];

And implement this method:

- (void)didPressButton:(UIButton *)sender
{
    UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
    [self.navigationController pushViewController:viewController animated:YES];
}
like image 138
Christian Schnorr Avatar answered Jan 11 '23 02:01

Christian Schnorr


If you use storyboard, create a segue manually

On storyboard:

  • click on the view
  • then on the right sidebar top menu select the "Show the connections inspector"
  • in the "Triggered Segues", click and drag the "manual" trigger to the view you want to push
  • chose "push" in the pop menu that will appear

it will create a segue, now select the segue and give it a identifier

Now you can use the code:

-(IBAction)doneAction:(id)sender
{
    [self performSegueWithIdentifier:@"identifier" sender:self ];
}

now you can wire the action to your button

like image 38
Adriano Spadoni Avatar answered Jan 11 '23 01:01

Adriano Spadoni