Как реализовать Navigation Controller, чтобы избавиться от создания кнопки «Назад»? - PullRequest
0 голосов
/ 04 сентября 2011

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

Какя могу это сделать?Я предполагаю, что я помещу Navigation Controller в MainWindow.xib.

Пожалуйста, помогите мне в редактировании приведенных ниже кодов, чтобы полностью реализовать Navigation Controller из приложения.

Полные кодыздесь (заголовок и реализация):

MainControllerView.h code

#import <UIKit/UIKit.h>

@class FirstView;

@class SecondView;

@interface MainControllerView : UIViewController {

   IBOutlet UILabel *label;
   IBOutlet UIButton *firstView;
   IBOutlet UIButton *secondView;

    FirstView *firstView1; 
    SecondView *secondView1;

}

@property(nonatomic,retain)  IBOutlet UILabel *label;

@property(nonatomic,retain)  IBOutlet UIButton *firstView;

@property(nonatomic,retain)  IBOutlet UIButton *secondView;


-(IBAction) FirstView:(id)sender;
-(IBAction) SecondView:(id)sender;



@end

MainControllerView.m

#import "MainControllerView.h"
#import "FirstView.h"
#import "SecondView.h"



@implementation MainControllerView

@synthesize label,firstView,secondView;



-(IBAction) FirstView:(id)sender

{

    firstView1 = [[FirstView alloc] 
                   initWithNibName:@"FirstView" 
                   bundle:nil];
    [self.view addSubview:firstView1.view];

}



-(IBAction) SecondView:(id)sender

{
    secondView1 = [[SecondView alloc] 
                  initWithNibName:@"SecondView" 
                  bundle:nil];
    [self.view addSubview:secondView1.view];
}





- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (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.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

@end

FirstView.h

#import <UIKit/UIKit.h>

@class MainControllerView;

@interface FirstView : UIViewController {

    IBOutlet UIButton *backButton;
    MainControllerView *mainControllerView;
}

@property(nonatomic,retain)  IBOutlet UIButton *backButton;


@end

FirstView.m

#import "FirstView.h"
#import "MainControllerView.h"


@implementation FirstView

@synthesize backButton;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (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.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5,18,100,40);
    [button setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];

    [self.view addSubview:button];


    // Do any additional setup after loading the view from its nib.
}


-(IBAction) BackAction:(id)sender

{

    mainControllerView = [[MainControllerView alloc] 
                      initWithNibName:@"MainControllerView"bundle:nil];

    [self.view addSubview:mainControllerView.view];
    [mainControllerView.view release];

}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

@end

SecondView.h

#import <UIKit/UIKit.h>

@class MainControllerView;

@interface SecondView : UIViewController {

    IBOutlet UIButton *backButton;
    MainControllerView *mainControllerView;
}

@property(nonatomic,retain)  IBOutlet UIButton *backButton;

@end

SecondView.m

#import "SecondView.h"
#import "MainControllerView.h"

@implementation SecondView

@synthesize backButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (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.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5,18,100,40);
    [button setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];

    [self.view addSubview:button];


    // Do any additional setup after loading the view from its nib.
}


-(IBAction) BackAction:(id)sender

{

    mainControllerView = [[MainControllerView alloc] 
                          initWithNibName:@"MainControllerView"bundle:nil];

    [self.view addSubview:mainControllerView.view];
    [mainControllerView.view release];

}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

@end

1 Ответ

2 голосов
/ 05 сентября 2011

В делегате приложения просто добавьте этот код в метод applicationDidFinishLoading.

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

[window makeKeyAndVisible];
[window addSubview:navController.view];

И не забудьте установить заголовок в вашем методе initWithNibName ...

self.title = @"Page Title";

Или вы можете начать свой проект, выбрав шаблон контроллера навигации, и все же не забудьте установить заголовок ...

...