iOS 导航控制器返回栈中的某一控制器

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
#import "AppDelegate.h"
#import "FirstViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    //初始化控制器
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
    self.window.rootViewController = navi;

    [self.window makeKeyAndVisible];
    return YES;
}

@end
#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"第一个控制器";

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(70, 100, 180, 45);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"跳到第二个控制器" forState:0];
    [button setTitleColor:[UIColor greenColor] forState:0];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonAction:(UIButton *)sender{
    [self.navigationController pushViewController:[[SecondViewController alloc] init] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.title = @"第二个控制器";

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(70, 100, 180, 45);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"跳到第三个控制器" forState:0];
    [button setTitleColor:[UIColor greenColor] forState:0];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonAction:(UIButton *)sender{
    [self.navigationController pushViewController:[[ThirdViewController alloc] init] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@end
#import "ThirdViewController.h"
#import "FourthViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"第三个控制器";
    self.view.backgroundColor = [UIColor yellowColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(70, 100, 180, 45);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"跳到第四个控制器" forState:0];
    [button setTitleColor:[UIColor greenColor] forState:0];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonAction:(UIButton *)sender{
    [self.navigationController pushViewController:[[FourthViewController alloc] init] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>

@interface FourthViewController : UIViewController

@end
#import "FourthViewController.h"
#import "FifthViewController.h"
@interface FourthViewController ()

@end

@implementation FourthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"第四个控制器";
    self.view.backgroundColor = [UIColor greenColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(70, 100, 180, 45);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"跳到第五个控制器" forState:0];
    [button setTitleColor:[UIColor greenColor] forState:0];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

- (void)buttonAction:(UIButton *)sender{
    [self.navigationController pushViewController:[[FifthViewController alloc] init] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import <UIKit/UIKit.h>

@interface FifthViewController : UIViewController

@end
#import "FifthViewController.h"
#import "ThirdViewController.h"
@interface FifthViewController ()

@end

@implementation FifthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"第五个控制器";
    self.view.backgroundColor = [UIColor blueColor];
    [self setupViews];
}
/**
 *  初始化视图
 */
- (void)setupViews{
    [self initializeButtonWithFrame:CGRectMake(70, 100, 180, 45) tag:1000 title:@"返回第一个控制器"];
    [self initializeButtonWithFrame:CGRectMake(70, 160, 180, 45) tag:1001 title:@"返回第二个控制器"];
    [self initializeButtonWithFrame:CGRectMake(70, 220, 180, 45) tag:1002 title:@"返回第三个控制器"];
    [self initializeButtonWithFrame:CGRectMake(70, 280, 180, 45) tag:1003 title:@"返回第四个控制器"];
}
/**
 *  初始化button
 *
 *  @param frame 尺寸
 *  @param tag   标签
 *  @param title button的标题
 */
- (void)initializeButtonWithFrame:(CGRect)frame tag:(NSInteger)tag title:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =frame;
    button.tag = tag;
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:title forState:0];
    [button setTitleColor:[UIColor whiteColor] forState:0];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
/**
 *  button触发的事件
 */
- (void)buttonAction:(UIButton *)sender{
//     在self.navigationController.viewControllers数组(的栈)中找到相应的控制器
    UIViewController *viewController = self.navigationController.viewControllers[sender.tag - 1000];
    [self.navigationController popToViewController:viewController animated:YES];

    //跳到某一控制器
    //遍历导航控制器(栈)中的控制器
//    for (UIViewController *controller in self.navigationController.viewControllers) {
//        // 找到相应的控制器
//        if ([controller isKindOfClass:[ThirdViewController class]]) {
//            //跳转到该控制器
//            [self.navigationController popToViewController:controller animated:YES];
//        }
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
时间: 2025-02-01 21:43:10

iOS 导航控制器返回栈中的某一控制器的相关文章

011实现一个栈,除了push和pop操作,还要实现min函数以返回栈中的最小值,时间复杂度都为O(1)(keep it up)

实现一个栈,除了push和pop操作,还要实现min函数以返回栈中的最小值. push,pop和min函数的时间复杂度都为O(1). 看到这个题目最直接的反应是用一个变量来保存当前栈的最小值,让我们来看看这样可行否? 如果栈一直push那是没有问题,入栈元素如果比当前最小值还小,那就更新当前最小值. 可是如果pop掉的栈顶元素就是最小值,那么我们如何更新最小值呢?显然不太好办. 既然只用一个变量没法解决这个问题,那我们就增加变量.如果说每个结点除了保存当前的 值, 另外再保存一个从该结点到栈底的

一、实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作

请指教交流! 1 package com.it.hxs.c01; 2 3 import java.util.Stack; 4 5 /* 6 实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作 7 */ 8 public class GetMinStack { 9 10 public static void main(String args[]) { 11 GetMinStack demoStack = new GetMinStack(); 12 demoStack.pus

将FragmentManger事务添加到返回栈中

FragmentManger事务添加或替换的 Fragment 后,这时点击 Back 键,程序并不会返回添加之前的状态. 我们可以使用 Transaction 对象的 addToBackStack() 方法,将事务添加到返回栈中,这样点击 Back 键时,就会返回该事务执行前的状态. /* * 在事务提交前,将事务添加到返回栈中 * 参数String name:接收一个名字用于描述返回栈的状态,一般传入 null 即可. * */ transaction.addToBackStack(null

Ios导航栏返回到指定的页面

在自己的项目实现中有这样的一个需求.一般情况下我们的导航栏返回按钮,是上个页面跳转过来,点击返回按钮返回到上来界面.但是在实际需求中有的并不是这么简单的.有的界面返回是只确定的界面.所以当时自己在实现的时候因为不太了解跳转的机制,导致无法实现.后来在老大,路哥(大牛)的知道下明白了.首先我们要知道导航栏的跳转是通过栈的形式进行的.所以我们每次跳转时,就会在栈里多出一个界面的对象.栈中的数就会增加,当我们需要返回跳转到前面的某一个界面时,需要我们在栈中寻找这个界面.我们需要一个数组来存放信息,当找

iOS 隐藏/去掉 导航栏返回按钮中的文字

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

iOS 导航栏返回的相关跳转

导航条跳转页面的考虑 对于用navigationcontroller来跳转页面的时候,其实是执行堆栈的进栈和出栈的操作,要想释放内存,那么在来回跳转的时候,就要考虑几个问题了 1 A =>B=>C=>D,D=>A 有根视图的话 (HOME)[self.navigationController popToRootViewControllerAnimated:YES];  D=>C  (每一个界面返回上一层)[self.navigationController popViewCo

SpringMVC中@Control控制器返回的是Jsp页面还是控制器请求的总结

1.返回到JSP页面: return "main" --跳转到 WEB-INF/main.jsp 页面. return "redirect:/main.jsp" --重定向到main.jsp页面,redirect无法访问WEB-INF下的资源文件,因此必须加上.jsp后缀. 2.转发控制器请求: return "forward:/main" --转到 为main的请求上. return "redirect:/main" --重

Android之Activity系列总结(二)--任务和返回栈

任务和返回栈 应用通常包含多个 Activity.每个 Activity 均应围绕用户可以执行的特定操作设计,并且能够启动其他 Activity. 例如,电子邮件应用可能有一个 Activity 显示新邮件的列表.用户选择某邮件时,会打开一个新 Activity 以查看该邮件. 一个 Activity 甚至可以启动设备上其他应用中存在的 Activity.例如,如果应用想要发送电子邮件,则可将 Intent 定义为执行“发送”操作并加入一些数据,如电子邮件地址和电子邮件. 然后,系统将打开其他应

android任务和返回栈

原文来自官方文档:https://developer.android.com/guide/components/tasks-and-back-stack.html 应用通常包含多个Activity.每个 Activity 均应围绕用户可以执行的特定操作设计,并且能够启动其他 Activity. 例如,电子邮件应用可能有一个 Activity 显示新邮件的列表.用户选择某邮件时,会打开一个新 Activity 以查看该邮件. 一个 Activity 甚至可以启动设备上其他应用中存在的 Activi