目标 C:Add/Perform 导航控制器中的多个 segue
ObjectiveC: Add/Perform multiple segue in navigation controller
我正在实现一个有 6-7 个屏幕流入的应用程序功能。并且用户可以 left/close 在任何屏幕上流动。
但是当用户再次申请时,他们应该跳转到他离开的最后一个屏幕,并且他可以回到之前的屏幕。
例如:我开始申请申请并完成到第 4 个屏幕并关闭。再次申请,我必须直接跳转到第 4 个屏幕,并且还可以从堆栈返回到第 3->2->1 个屏幕。
当前代码:
Storyboard
中 1-7 屏幕的 Segue 标识符为 "screen1"、"screen1" ... "screen7"
来自HomeScreen.m
-(void)toPersonalApplication {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Personal" bundle:nil];
ScreenOne *screenOne = [storyboard instantiateViewControllerWithIdentifier:@"screenOne"];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:screenOne];
[self presentViewController:nav animated:YES completion:nil];
}
正在检查用户是否已经启动了申请流程:
在 ScreenOne.m
- (IBAction)btnNextClick:(id)sender {
if (doneProcessTill == 4) {
// Should be execute something like this here
// [self performSegueWithIdentifier:@"screen2" sender:self];
// [self performSegueWithIdentifier:@"screen3" sender:self];
// [self performSegueWithIdentifier:@"screen4" sender:self];
}
}
感谢您的建议!
谢谢
如我的评论所述,解决方案可能如下所示:
- (void)toPersonalApplication {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Personal" bundle:nil];
NSMutableArray *viewControllers = [NSMutableArray array];
for (NSInteger i = 1; i <= doneProcessTill; ++i) {
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"screen%ld", (long)i]];
[viewControllers addObject:viewController];
}
UINavigationController *navigationController = [UINavigationController new];
navigationController.viewControllers = viewControllers;
[self presentViewController:navigationController animated:YES completion:nil];
}
对于这种情况,最好保存视图控制器,然后在您想返回到原来的位置时再次显示它们。在任何时候,您都可以使用 navigationController.viewControllers
获取当前的视图控制器。要设置整个堆栈,您只需在导航控制器上调用 setViewControllers
。
您可以覆盖导航控制器来存储控制器数组。或者您可以静态保存它。以您的情况更方便为准。
静态的看起来像这样:
static NSArray<UIViewController *> *__preservedControllers = nil;
@implementation ViewController
- (void)onClose {
__preservedControllers = self.navigationController.viewControllers;
// Insert logic to close the whole procedure
}
- (void)reopenSavedStack {
if(__preservedControllers) {
[self.navigationController setViewControllers:__preservedControllers animated:YES];
__preservedControllers = nil;
}
}
使用子类化导航控制器它可能看起来更好:
@interface MyNavigationController : UINavigationController
@property (nonatomic, strong) NSArray<UIViewController *> *savedStack;
@end
@implementation MyNavigationController
- (void)saveStack {
self.savedStack = self.viewControllers;
}
- (void)restoreStack:(BOOL)animated {
if(self.savedStack.count > 0) {
[self setViewControllers:self.savedStack animated:YES];
self.savedStack = nil;
}
}
@end
但是你确实需要进行类型转换:
- (void)onClose {
[(MyNavigationController *)self.navigationController saveStack];
// Insert logic to close the whole procedure
}
- (void)reopenSavedStack {
[(MyNavigationController *)self.navigationController restoreStack:YES];
}
我正在实现一个有 6-7 个屏幕流入的应用程序功能。并且用户可以 left/close 在任何屏幕上流动。
但是当用户再次申请时,他们应该跳转到他离开的最后一个屏幕,并且他可以回到之前的屏幕。
例如:我开始申请申请并完成到第 4 个屏幕并关闭。再次申请,我必须直接跳转到第 4 个屏幕,并且还可以从堆栈返回到第 3->2->1 个屏幕。
当前代码:
Storyboard
中 1-7 屏幕的 Segue 标识符为 "screen1"、"screen1" ... "screen7"
来自HomeScreen.m
-(void)toPersonalApplication {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Personal" bundle:nil];
ScreenOne *screenOne = [storyboard instantiateViewControllerWithIdentifier:@"screenOne"];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:screenOne];
[self presentViewController:nav animated:YES completion:nil];
}
正在检查用户是否已经启动了申请流程:
在 ScreenOne.m
- (IBAction)btnNextClick:(id)sender {
if (doneProcessTill == 4) {
// Should be execute something like this here
// [self performSegueWithIdentifier:@"screen2" sender:self];
// [self performSegueWithIdentifier:@"screen3" sender:self];
// [self performSegueWithIdentifier:@"screen4" sender:self];
}
}
感谢您的建议! 谢谢
如我的评论所述,解决方案可能如下所示:
- (void)toPersonalApplication {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Personal" bundle:nil];
NSMutableArray *viewControllers = [NSMutableArray array];
for (NSInteger i = 1; i <= doneProcessTill; ++i) {
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"screen%ld", (long)i]];
[viewControllers addObject:viewController];
}
UINavigationController *navigationController = [UINavigationController new];
navigationController.viewControllers = viewControllers;
[self presentViewController:navigationController animated:YES completion:nil];
}
对于这种情况,最好保存视图控制器,然后在您想返回到原来的位置时再次显示它们。在任何时候,您都可以使用 navigationController.viewControllers
获取当前的视图控制器。要设置整个堆栈,您只需在导航控制器上调用 setViewControllers
。
您可以覆盖导航控制器来存储控制器数组。或者您可以静态保存它。以您的情况更方便为准。
静态的看起来像这样:
static NSArray<UIViewController *> *__preservedControllers = nil;
@implementation ViewController
- (void)onClose {
__preservedControllers = self.navigationController.viewControllers;
// Insert logic to close the whole procedure
}
- (void)reopenSavedStack {
if(__preservedControllers) {
[self.navigationController setViewControllers:__preservedControllers animated:YES];
__preservedControllers = nil;
}
}
使用子类化导航控制器它可能看起来更好:
@interface MyNavigationController : UINavigationController
@property (nonatomic, strong) NSArray<UIViewController *> *savedStack;
@end
@implementation MyNavigationController
- (void)saveStack {
self.savedStack = self.viewControllers;
}
- (void)restoreStack:(BOOL)animated {
if(self.savedStack.count > 0) {
[self setViewControllers:self.savedStack animated:YES];
self.savedStack = nil;
}
}
@end
但是你确实需要进行类型转换:
- (void)onClose {
[(MyNavigationController *)self.navigationController saveStack];
// Insert logic to close the whole procedure
}
- (void)reopenSavedStack {
[(MyNavigationController *)self.navigationController restoreStack:YES];
}