您的位置:首页 > 其它

通过导航栏进入多个视图

2016-05-02 13:44 375 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: