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

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

相关文章

js+css二级导航

效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con

通俗范畴论4 范畴的定义

注:由于CSDN无法显示本文章源文件的公式,因此部分下标、字母花体、箭头表示可能会不正常,请读者谅解 范畴的正式定义 上一节我们在没有引入范畴这个数学概念的情况下,直接体验了一个“苹果1”范畴,建立了一个对范畴的直观。本节我们正式学习范畴的定义和基本性质。 一个范畴(Category) C𝐶,由以下部分组成: 数据: 对象(Objects):包含若干个对象(Objects),这些

MySQL数据库(四):视图和索引

在数据库管理中,视图和索引是两种关键工具,它们各自发挥独特的作用以优化数据查询和管理。视图通过简化复杂查询、提高数据安全性和提供数据抽象,帮助用户轻松访问数据。而索引则通过加速查询、确保数据唯一性以及优化排序和分组操作,显著提升数据库性能。理解和合理运用这两者,对数据库系统的高效运行至关重要。 目录 一、视图概念(面试) 二、视图的作用(面试) 三、视图的创建和使用 3.1

FastAdmin/bootstrapTable 表格中生成的按钮设置成文字

公司有个系统后台框架用的是FastAdmin,后台表格的操作栏按钮只有图标,想要设置成文字。 查资料后发现其实很简单,主需要新增“text”属性即可,如下 buttons: [{name: 'acceptcompany',title: '复核企业',text:'复核企业',classname: 'btn btn-xs btn-primary btn-dialog',icon: 'fa fa-pe

利用结构体作为函数参数时结构体指针的定义

在利用结构体作为函数的参数进行传递时,容易犯的一个错误是将一个野指针传给函数导致错误。 #include <stdio.h>#include <math.h>#include <malloc.h>#define MAXSIZE 10typedef struct {int r[MAXSIZE]; //用于存储要排序的数组,r[0]作为哨兵或者临时变量int length;

ccp之间是不可以直接进行+,-的,要用ccpSub和ccpAdd。

1.  http://www.cnblogs.com/buaashine/archive/2012/11/12/2765691.html  上面有好多的关于数学的方面的知识,cocos2dx可能会用到的 2.学到了   根据tilemap坐标得到层上物体的id int oneTiled=flagLayer->tileGIDt(tilePos);

OpenCV中的按钮问题

在HighGUI中,没有显示提供任何形式的按钮。一般有两种方法替代: 1.用只有两个状态的滑动条来替代按钮。开关(switch)事实上就是只有两个状态的滑动条,这两个状态是on和off。然后通过回调函数来实现相关的功能。 实例源码(使用滑动条实现一个开关功能) #include<cv.h>#include<highgui.h>int g_switch_value = 0;void swit

redis切换数据库的方法【jedis】

package com.test;import redis.clients.jedis.Jedis;public class readredis {public static void main(String[] args) {// 连接本地的 Redis 服务Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(10);String v

1_CString char* string之间的关系

CString转char*,string string转char*,CString char* 转CString,string 一、CString转char*,string //字串转换测试 CString CString1; std::string string1; CHAR* char1=NULL; //1string1=CString1.GetBuffer();CStri

一二三应用开发平台应用开发示例(4)——视图类型介绍以及新增、修改、查看视图配置

调整上级属性类型 前面为了快速展示平台的低代码配置功能,将实体文件夹的数据模型上级属性的数据类型暂时配置为文本类型,现在我们调整下,将其数据类型调整为实体,如下图所示: 数据类型需要选择实体,并在实体选择框中选择自身“文件夹” 这时候,再点击生成代码,平台会报错,提示“实体【文件夹】未设置主参照视图”。这是因为文件夹选择的功能页面,同样是基于配置产生的,因为视图我们还没有配置,所以会报错。