OneViewController.m
#import "OneViewController.h"
#import "TwoViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger number =[self.navigationController.viewControllers count];
self.title = [NSString stringWithFormat:@"第%ld个视图", number];
}
- (IBAction)buttonAction:(UIButton *)sender {
TwoViewController *two = [[TwoViewController alloc] init];
[self.navigationController pushViewController:two animated:YES];
}
@end
TwoViewController.m
#import "TwoViewController.h"
#import "ThreeViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger number =[self.navigationController.viewControllers count];
self.title = [NSString stringWithFormat:@"第%ld个视图", number];
}
- (IBAction)buttonAction:(UIButton *)sender {
UINavigationController *nc = self.navigationController;
ThreeViewController *three = [[ThreeViewController alloc] init];
switch (sender.tag) {
case 1:
[nc pushViewController:three animated:YES];
break;
case 2:
[nc popToRootViewControllerAnimated:YES];
break;
default:
break;
}
}
@end
ThreeViewController.m
#import "ThreeViewController.h"
@interface ThreeViewController ()
@end
@implementation ThreeViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger number =[self.navigationController.viewControllers count];
self.title = [NSString stringWithFormat:@"第%ld个视图", number];
}
- (IBAction)buttonAction:(UIButton *)sender {
UINavigationController *nc = self.navigationController;
ThreeViewController *three = [[ThreeViewController alloc] init];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"请输入index" message:[NSString stringWithFormat:@"1-%@", self.title]delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
switch (sender.tag) {
case 1:
[nc pushViewController:three animated:YES];
break;
case 2:
[nc popViewControllerAnimated:YES];
break;
case 3:
[nc popToRootViewControllerAnimated:YES];
break;
case 4:
[alertView show];
break;
default:
break;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSString *str = [alertView textFieldAtIndex:0].text;
NSInteger i = [str integerValue];
UINavigationController *nc = self.navigationController;
[nc popToViewController:nc.viewControllers[i-1] animated:YES];
}
@end