开发者

Can't add UINavigationController programmatically to a TabBar Application

开发者 https://www.devze.com 2023-03-29 06:24 出处:网络
I have an application for which I use TabBar template. In one of viewcontrollers I want to add a uinavigationcontroller. I declare it in the .h file;

I have an application for which I use TabBar template. In one of viewcontrollers I want to add a uinavigationcontroller. I declare it in the .h file;

#import <UIKit/UIKit.h>
#import "AnotherViewController.h"

@interface SecondViewController : UIViewController <UINavigationControllerDelegate> {
    UIButton *UIButton *gotoAnotherView;;
    AnotherViewController *anotherView;
    UINavigationController *navigationController;
}

@property(nonatomic,retain) UIButton *UIButton *gotoAnotherView;;
@property(nonatomic,retain) AnotherViewController *anot开发者_如何学JAVAherView;
@property(nonatomic,retain) UINavigationController *navigationController;

-(void)buttonPressed:(id)sender;

@end

And here's my .m file

#import "SecondViewController.h"


@implementation SecondViewController

@synthesize navigationController, anotherView, gotoAnotherView;


-(void)buttonPressed:(id)sender {

    anotherView = [[AnotherViewController alloc]init];
    [navigationController pushViewController:anotherView animated:YES];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

    navigationController = [[UINavigationController alloc ]initWithRootViewController:self];
    [navigationController.navigationBar setFrame:CGRectMake(0, 0, 320, 44)];

    [self.view addSubview:navigationController.navigationBar];

    gotoAnotherView = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 40, 40)]; //kategoributonlari

    UIImage *image = [UIImage imageNamed:@"1.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(110, 5, 100, 20);
    [self.view addSubview:imageView];

    [kategori1 setBackgroundImage:image forState:UIControlStateNormal];

    [kategori1 addTarget:self 
               action:@selector(buttonPressed:)
     forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:kategori1];

    [super viewDidLoad];

}




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

However I can see from the navigation bar that the navigationcontroller goes one level deeper(back button appears) but the main view remains the same with my gotoAnotherView button.

I think that I might not make the navigationcontroller control the whole view.


Instead of trying to do this in code, edit the XIB for your main window (with the UITabBarController). Drag out a UINavigationController from the Library onto the tab bar. This will create a new bar item for you, with a UINavigationController. Select the UIViewController nested in the new UINavigationController, and on the Identity tab set the Class to your view controller, and on the Attributes tab specify the name of the nib file to load.


You don't need to use IB. You can setup everything in code. First create your view controllers tab1ViewController, tab2ViewController, etc. then create the navigation controller with the root view controllers of tab1ViewController etc. and then add these controllers to the tab bar controller.

Here is a sample:

UINavigationController *tab1NavigationController = [[UINavigationController alloc] initWithRootViewController:tab1ViewController];
UINavigationController *tab2NavigationController = [[UINavigationController alloc] initWithRootViewController:tab2ViewController];
UITabBarController rootViewController = [[UITabBarController alloc] init];
rootViewController.viewControllers = [NSArray arrayWithObjects:tab1NavigationController, tab2NavigationController, nil];
[tab1NavigationController release];
[tab2NavigationController release];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号