1

我有一個使用TabBar模板的應用程序。在其中一個視圖控制器中,我想添加一個uinavigationcontroller。我在.h文件中聲明它;無法將UINavigationController以編程方式添加到TabBar應用程序

#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; 
} 

但是我可以從該navigationcontroller深入一層導航欄看到(後面會出現按鈕),但主要觀點仍與我gotoAnotherView相同按鈕。

我想我可能不會讓navigationcontroller控制整個視圖。我會很感激任何幫助。

謝謝。

+0

爲什麼性能也聲明爲成員變量? –

回答

1

而不是嘗試在代碼中執行此操作,編輯主窗口的XIB(使用UITabBarController)。從庫中拖出一個UINavigationController到標籤欄。這將使用UINavigationController爲您創建一個新的酒吧項目。選擇嵌套在新UINavigationController中的UIViewController,然後在Identity選項卡上將Class設置爲視圖控制器,並在Attributes選項卡上指定要加載的nib文件的名稱。

+0

很酷。我討厭使用IB,但至少在主佈局上,我可以使用它。 =) –

+1

@Hasan你真的應該讀這篇[與Aaron Hillegass的訪談](http://www.informit.com/articles/article.aspx?p=1220319)。特別是問題的答案**您認爲新的Mac開發人員來自Windows,Java甚至傳統C++ GUI工具包的最常見誤解是什麼?**使用鏈鋸。 – SSteve

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]; 
相關問題