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

2024-06-19 02:08

本文主要是介绍导航视图控制器中如何定义按钮以及视图之间的切换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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];               //返回到第一个视图界面

}

这篇关于导航视图控制器中如何定义按钮以及视图之间的切换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073712

相关文章

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot实现多环境配置文件切换

《SpringBoot实现多环境配置文件切换》这篇文章主要为大家详细介绍了如何使用SpringBoot实现多环境配置文件切换功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 示例代码结构2. pom文件3. application文件4. application-dev文

详解如何在SpringBoot控制器中处理用户数据

《详解如何在SpringBoot控制器中处理用户数据》在SpringBoot应用开发中,控制器(Controller)扮演着至关重要的角色,它负责接收用户请求、处理数据并返回响应,本文将深入浅出地讲解... 目录一、获取请求参数1.1 获取查询参数1.2 获取路径参数二、处理表单提交2.1 处理表单数据三、

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾