iOS开发导航控制器跳转和返回指定控制器

2017-08-26  本文已影响16人  lczalh
#import "SecViewController.h"

@interface SecViewController ()

@end

@implementation SecViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"第个%ld视图",self.navigationController.viewControllers.count];
    [self createBtn];
    // Do any additional setup after loading the view.
}
//创建按钮
- (void)createBtn{
    NSArray *ary = @[@"push",@"pop",@"root",@"index"];
    SEL act[4] = {@selector(tiaoZhuan1),@selector(tiaoZhuan2),@selector(tiaoZhuan3),@selector(tiaoZhuan4)};
    for (int i=0; i<4; i++) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100*i+100, 100, 100)];
        [self.view addSubview:btn];
        [btn setTitle:ary[i] forState:UIControlStateNormal];
        [btn addTarget:self action:act[i] forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void)tiaoZhuan1{
    //跳转到当前页面。
    [self.navigationController showViewController:[[SecViewController alloc]init] sender:self];
}
- (void)tiaoZhuan2{
    //返回上一页面
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tiaoZhuan3{
    //返回根控制器
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)tiaoZhuan4{
    //创建一个弹出框
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleAlert];
    //添加弹出框上的文本框
    [ac addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入跳转到的页数";
        textField.keyboardType = UIKeyboardTypeNumberPad;
    }];
    //添加弹出框上的按钮
    [ac addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        //总视图大小
        long str1 = self.navigationController.viewControllers.count;
        //输入的数字
        long str2 = [ac.textFields.firstObject.text intValue];
        //或 UITextField *text = ac.textFields[0]
        if (str2<str1) {
            
                    [self.navigationController popToViewController:self.navigationController.viewControllers[str2-1] animated:YES];
            
        }
        
        if (str2>str1) {
            UIAlertController *ac2 = [UIAlertController alertControllerWithTitle:@"⚠警告" message:@"您输入的页数超过总控制器页数,请重新输入!" preferredStyle:UIAlertControllerStyleAlert];
            [ac2 addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self tiaoZhuan4];
            }]];
            [self presentViewController:ac2 animated:YES completion:nil];
        }
        
        
    }]];
    [self presentViewController:ac animated:YES completion:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读