如何实现导航控制器以摆脱创建后退按钮?

发布于 2024-12-02 17:33:13 字数 6740 浏览 0 评论 0原文

我想使用导航控制器来帮助我导航到第一个视图和第一个视图。主控制器视图中的第二个视图,并且无需创建“后退”按钮,因此导航控制器将为我处理它。

我该怎么做?我假设我会将导航控制器放在 MainWindow.xib 中。

请指导我编辑下面的代码,以从应用程序中完全实现导航控制器。

完整代码在这里(头文件和实现):

MainControllerView.h 代码

#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

I want to use a Navigation Controller to help me navigate to the First View & Second View from Main Controller View and eliminating the need to create a Back button so Navigation Controller will handle it for me.

How can I do this? I'm assuming that I'll put the Navigation Controller in MainWindow.xib.

Please guide me in editing the codes below to fully implement the Navigation Controller from the app.

Complete codes are here (both header and implementation):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2024-12-09 17:33:13

在您的应用程序委托中,只需在 applicationDidFinishLoading 方法上添加此代码即可。

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

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

并且不要忘记在 initWithNibName 方法中设置标题...

self.title = @"Page Title";

或者您可以通过选择导航控制器模板来启动项目,但仍然不要忘记设置标题...

In your app delegate, just add this code on applicationDidFinishLoading method.

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

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

And dont forget to set the title in your initWithNibName method...

self.title = @"Page Title";

Or you can start your project by choosing the navigation controller template and still dont forget to set the title...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文