Не удается программно добавить UINavigationController в приложение TabBar - PullRequest
1 голос
/ 20 августа 2011

У меня есть приложение, для которого я использую шаблон TabBar.В одном из viewcontrollers я хочу добавить контроллер uinavigation.Я заявляю об этом в файле .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.

Я думаю, что я не могу заставить навигационный контроллер управлять всем видом.

Ответы [ 2 ]

1 голос
/ 20 августа 2011

Вам не нужно использовать 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];
1 голос
/ 20 августа 2011

Вместо того, чтобы пытаться сделать это в коде, отредактируйте XIB для вашего главного окна (с помощью UITabBarController). Перетащите UINavigationController из библиотеки на панель вкладок. Это создаст для вас новый элемент панели с UINavigationController. Выберите UIViewController, вложенный в новый UINavigationController, и на вкладке Identity установите класс для вашего контроллера представления, а на вкладке Attributes укажите имя файла пера для загрузки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...