我有一个应用程序,我使用TabBar模板。在一个viewcontrollers中,我想添加一个uinavigationcontroller。我在.h文件中声明它;
#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 *anotherView;
@property(nonatomic,retain) UINavigationController *navigationController;
-(void)buttonPressed:(id)sender;
@end
这是我的.m文件
#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;
}
但是我可以从导航栏看到导航控制器更深一层(出现后退按钮),但主视图与我的gotoAnotherView按钮保持一致。
我认为我可能不会让导航控制器控制整个视图。
答案 0 :(得分:1)
不要在代码中尝试这样做,而是编辑主窗口的XIB(使用UITabBarController)。将UINavigationController从库中拖出到选项卡栏上。这将使用UINavigationController为您创建一个新的条形项。选择嵌套在新UINavigationController中的UIViewController,并在Identity选项卡上将Class设置为视图控制器,并在Attributes选项卡上指定要加载的nib文件的名称。
答案 1 :(得分:1)
您不需要使用IB。您可以在代码中设置所有内容。首先创建视图控制器tab1ViewController,tab2ViewController等,然后使用tab1ViewController等的根视图控制器创建导航控制器,然后将这些控制器添加到标签栏控制器。
以下是一个示例:
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];