我有一个标准的 UINavigation 控制器,可以像往常一样使用 push 和 pop 来推送我的屏幕。但是,我有一个屏幕可以在按下按钮时在两个视图控制器之间切换,因此屏幕翻转到顶部会显示另一个视图控制器,反之亦然。您可以随意在它们之间切换。
现在,我希望后退按钮正常工作,因此我交换顶视图控制器以实现此目的,如下所示:
-(IBAction)switchToSlowProductEntry:(id)sender
{
NSLog(@"Switching to slow product entry");
// Replace the top view with the findProductView
FindProductView *findProdView = [ProductViewInstances shared].findProductView;
NSMutableArray *views = [self.navigationController.viewControllers mutableCopy];
[views removeLastObject];
[views addObject:findProdView];
// if sender is nil then assume we started from the viewDidLoad so no animation
if(sender)
{
[UIView transitionWithView:self.navigationController.view duration:0.3 options:UIViewAnimationOptionTransitionFlipFromRight animations:^
{
[self.navigationController setViewControllers:views animated:NO];
}
completion:^(BOOL finished) {}];
}
else
[self.navigationController setViewControllers:views animated:NO];
NSLog(@"Views: %@", views);
[views release];
[ProductViewInstances shared].lastScreen = SlowProductEntryView;
}
-(IBAction)switchToQuickProductEntry:(id)sender
{
NSLog(@"Switching to fast product entry");
// Replace the top view with the findProductView
QuickOrderEntryView *quickProductView = [ProductViewInstances shared].quickProductView;
NSMutableArray *views = [self.navigationController.viewControllers mutableCopy];
[views removeLastObject];
[views addObject:quickProductView];
if(sender)
{
[UIView transitionWithView:self.navigationController.view duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^
{
[self.navigationController setViewControllers:views animated:NO];
}
completion:^(BOOL finished) {}];
}
else
[self.navigationController setViewControllers:views animated:NO];
NSLog(@"Views: %@", views);
[views release];
[ProductViewInstances shared].lastScreen = QuickProductEntryView;
}
对于另一个屏幕,我有一段类似的代码。我正在使用 ProductViewInstances 类来维护两个视图控制器,因为我不希望在我在屏幕上维护阶段时卸载这些类。
当你想从这些屏幕向前移动时,我会像往常一样推送到新屏幕。它有效,我在查看了我添加的产品后返回。如果我按回,我会回到上面的屏幕,一切看起来都很正常。但是,当我按下自定义后退按钮(如果按下后我需要进行处理)时,我遇到了问题。
popViewController 什么都不做。这是基类中用于管理自定义后退按钮的代码。
-(void) viewDidLoad
{
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil)
style:UIBarButtonItemStyleBordered
target:self
action:@selector(myCustomBack)] autorelease];
if(![ProductViewInstances shared].findProductView)
{
[ProductViewInstances shared].findProductView = [[FindProductView alloc] init];
[ProductViewInstances shared].findProductView.customer = self.customer;
}
if(![ProductViewInstances shared].quickProductView)
{
[ProductViewInstances shared].quickProductView = [[QuickOrderEntryView alloc] init];
[ProductViewInstances shared].quickProductView.customer = self.customer;
}
}
-(void) goBack
{
if([[ProductViewInstances shared].quickProductView checkIfItemsPending])
{
// Pop up dialog
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Save Entries", nil)
message:NSLocalizedString(@"Your entries will be lost", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
otherButtonTitles:NSLocalizedString(@"Save", nil), nil];
[alert show];
[alert release];
}
else
{
// Remove rows from quick item entry screen
[[ProductViewInstances shared].quickProductView removeRowsFromtable];
if(didPressHome)
[self popToSpringBoard:YES];
else
[self.navigationController popViewControllerAnimated:YES];
}
}
因此,当我回击时,我必须检查条目是否会丢失。弹出到 SpringBoard 会弹出几个屏幕并基本上调用以下内容:
NSArray *controllers = appDelegate.navigationController.viewControllers;
UIViewController *springboard = [controllers objectAtIndex:2];
[appDelegate.navigationController popToViewController:springboard animated:animated];
然而,popViewController 动画调用什么也没做……就好像它从未发生过一样。
多尼