导航视图控制器中如何定义按钮以及视图之间的切换

UINavigationController   相关知识总结

在"NavigationAppDelegate.m"文件中:包含"FirstViewController.h"头文件

在-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    FirstViewController* firstViewController = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:firstViewController] autorelease];

    self.window.rootViewController = self.navigationController;

    [self.window makeKeyAndVisible];

    return YES;

}

运行程序,会发现有一个导航栏

然后在"FirstViewController.m"文件中,包含"SecondViewController.h"头文件

-(void)viewDidLoad

{

    [super viewDidLoad];

    //采用自定义按钮的方法,给导航栏定义一个右侧按钮

    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(0,0,60,40);

    [button setTitle:@"exchange" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(exchange:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem* right = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

    self.navigationItem.rightBarButtonItem = right;


    //采用系统方法,给导航栏增加一个左侧按钮 

    UIBarButtonItem* left = [[[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleDone target:@selector(left:)] autorelease];

    self.navigationItem.leftBarButtonItem = left;


    //给当前视图的导航栏设置标题

    self.navigationItem.title = @"一级视图";

}


-(void)exchange:(id)sender         //单击定义导航栏右侧的按钮,进入到下一个视图。

{

    SecondViewController* secondViewController = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];

    [self.navigationController pushViewController:secondViewController animated:YES];

}


然后在"SecondViewController.m"文件中,包含"ThirdViewController.h"头文件

- (void)viewDidLoad

{

    [super viewDidLoad];

    //定义一个右侧按钮

    UIBarButtonItem* rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStyleDone target:@selector(next:)] autorelease];

    self.navigationItem.rightBarButtonItem = rightButton;

}


- (void)next:(id)sender       //单击定义二级视图右侧的按钮,进入到三级视图。

{

    ThirdViewController* thirdViewController = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease];

    [self.navigationController pushViewController:thirdViewController animated:YES];

}


然后在文件"ThirdViewController.h"中,使用xib定义四个按钮,分别对应下面四个方法

- (void)push:(id)sender     //单击此按钮,进入到下一个视图

{

    ThirdViewController* thirdViewController = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease];

    [self.navigationController pushViewController:thirdViewController animated:YES];

}


- (void)pop:(id)sender      //单击此按钮,返回到上一个视图

{

    [self.navigationController popViewControllerAnimated:YES];

}


- (void)popToRoot:(id)sender           //单击此按钮,返回到根视图界面

{

    [self.navigationController popToRootViewControllerAnimated:YES];

}


- (void)popToIndex:(id)sender        //返回到指定位置的视图

{

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];               //返回到第一个视图界面

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值