dismissViewController:不工作

我有一个名为vc0的视图控制器,它是这样呈现的:

[self presentViewController: vc1 animated: YES completion: nil]; 

而在VC1我有一个button来呈现另一个视图控制器:

 [self presentViewController: vc2 animated: YES completion: nil]; 

然后在vc2中,我有一个button来closures视图控制器:

 [self dismissViewControllerAnimated:YES completion: ^{ // over here I call one method in vc1 } 

和预期的一样,它会返回到vc1 ..但是在vc1中有一个button可以通过closures视图控制器来返回到vc0,如下所示:

  [self dismissViewControllerAnimated:YES completion:nil]; 

但由于某种原因,它不起作用,视图控制器不会被解雇回vc0。 当我第一次出现vc1时,我可以按下button来closures视图控制器,它的工作原理。 但是当我按下button打开vc2,并且当我将vc2退回到vc1时,那么我按下buttonclosures视图控制器,即当它不工作时。

对不起,如果这个问题有点不清楚,我想说一下我想说的话有点难。

还有一件事:

我尝试更换dismissViewControllerAnimated:dismissViewControllerAnimated:手动呈现vc0,但后来我在控制台中得到一个日志,说我想呈现一个vc0,但vc1的视图不在窗口层次结构。 这是什么意思?

感谢帮助!

更新:

在这种情况下,VC0是MenuMileIndexViewController – VC1是FlightViewController – VC2是BookmarksTableViewController

这是涉及的代码:

MenuMileIndexViewController:

 - (IBAction)goToOriginPage { FlightRecorder *origin = [[FlightRecorder alloc] init]; [self presentViewController:origin animated:YES completion:nil]; } 

飞行logging器:

  - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar { [self bringUpBookmarkkTable]; } - (void) bringUpBookmarkkTable { BookmarkTableViewController *bookmarkTVC = [[BookmarkTableViewController alloc] init]; [bookmarkTVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal]; [self presentViewController:bookmarkTVC animated:YES completion:nil]; } - (IBAction)cancel { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)endBookmarkProcessWithBookmarkCollection: (NSDictionary *)dict { presetBookmarkContext = [dict mutableCopy]; bookmarkMode = YES; NSString *compiledText = nil; NSNumber *number1 = [NSNumber numberWithInt: 1]; if ([dict objectForKey: @"bookmarkTag"] == number1) { compiledText = [NSString stringWithFormat: @"%@ to %@", [dict objectForKey: @"origin"], [dict objectForKey: @"destination"]]; } else { compiledText = [NSString stringWithFormat: @"%@ to %@", [dict objectForKey: @"destination"], [dict objectForKey: @"origin"]]; } compiledText = [compiledText stringByReplacingOccurrencesOfString:@"Origin: " withString:@""]; compiledText = [compiledText stringByReplacingOccurrencesOfString:@"Destination: " withString:@""]; flightContext = [NSDictionary dictionaryWithObjectsAndKeys: [dict objectForKey: @"miles"], @"miles", compiledText, @"location", [[NSUserDefaults standardUserDefaults] objectForKey: @"tempD"], @"date", nil]; NSString *string = [NSString stringWithFormat: @"\nMiles: %.2f\nFlight: %@\nDate: %@", [[dict objectForKey: @"miles"] floatValue], compiledText, [[NSUserDefaults standardUserDefaults] objectForKey:@"tempD"]]; UIAlertView *bvkBookmarkAlertView = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:string delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; [bvkBookmarkAlertView show]; } - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { [self cancel]; // Even though cancel is an IBAction, IBAction is the same thing as void so it is callable } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { [TheMileIndexViewController addDesiredMilesToIndex: [[flightContext objectForKey: @"miles"] doubleValue]]; [TravelLogViewController addFlight: flightContext]; if (!bookmarkMode) { if ([checkbox isSelected]) { [BookmarkHandler uploadBookmark: bookmarkFlightContext]; } } } if (buttonIndex == 0) { if ([alertView.title isEqualToString: @"Confirmation"]) { bookmarkMode = NO; } } } 

BookmarksTableViewController:

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated: YES]; NSDictionary *dict = [[BookmarkHandler bookmarkCollection] objectAtIndex: indexPath.row]; fl = [[FlightRecorder alloc] init]; [self dismissViewControllerAnimated:YES completion:^{ [fl endBookmarkProcessWithBookmarkCollection: dict]; }]; } 

现在,我已经在模拟器中创build了应用程序的屏幕录像,显示出问题所在。 我可以通过电子邮件发送给你参考。 所以我可以通过电子邮件发送给你。

我会build议总是从风险投资公司撤回风险投资 – 使用代表。 这实际上也是苹果公司推荐的方式 – 就像我之前对这个问题的回答所指出的那样。

所以如果你有VC0提供VC1,也用VC0在VC0中解除VC1代码,使用委托scheme。

我了解到,这是处理提交和解雇的最好办法 – 尽pipe有时候它会在VC1内解散VC1。

我曾经问过一个非常相关的问题,你可能也有兴趣去检查一下。 它显示代码…

我也读了一些解雇VC1 – 这又将解雇VC2。 但是,如果我以前的build议有效,我不会这样做。 有时候我会在执行(没有崩溃)的时候得到信息,说VC不再存在,或者任何与之相关的东西 – 所以我认为这不是最好的解决scheme。 但如果我先前的build议不起作用,你可以尝试第二个。

虽然不能保证这会持续更新到新的iOS,因为这个问题不断的haju我几个iOS更新现在:-),所以我决定去标准的推荐路线。

编辑:这是我修改的代码 – 它的工作没有问题 – 但不清楚你打算发生后,用户回应的警报,是否警报应在VC1或VC0。 无论如何,使用委托和callback我没有看到任何问题。 请解释一下我是否错过了你的观点

FlightViewControllerProtocol.h

 @protocol FlightViewControllerProtocol <NSObject> -(void) dismissVCAndEndBookmark; @end 

FlightViewController.m

 #import "FlightViewController.h" #import "FlightViewControllerProtocol.h" #import "BookmarksTableViewController.h" @interface FlightViewController () @end @implementation FlightViewController @synthesize delegate; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar { [self bringUpBookmarkkTable]; } - (IBAction) bringUpBookmarkkTable { BookmarksTableViewController *bookmarkTVC = [[BookmarksTableViewController alloc] init]; bookmarkTVC.delegate = self; [bookmarkTVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal]; [self presentViewController:bookmarkTVC animated:YES completion:nil]; } - (IBAction)cancel { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)endBookmarkProcessWithBookmarkCollection: (NSDictionary *)dict { // presetBookmarkContext = [dict mutableCopy]; // bookmarkMode = YES; NSString *compiledText = nil; NSNumber *number1 = [NSNumber numberWithInt: 1]; if ([dict objectForKey: @"bookmarkTag"] == number1) { compiledText = @"Text1"; } else { compiledText = @"Text2"; } // flightContext = [NSDictionary dictionaryWithObjectsAndKeys: [dict objectForKey: @"miles"], @"miles", compiledText, @"location", [[NSUserDefaults standardUserDefaults] objectForKey: @"tempD"], @"date", nil]; NSString *string = compiledText; UIAlertView *bvkBookmarkAlertView = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:string delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; [bvkBookmarkAlertView show]; } - (void) dismissVCAndEndBookmark { [self dismissViewControllerAnimated:YES completion:nil]; [self endBookmarkProcessWithBookmarkCollection: nil]; } - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { [self cancel]; // Even though cancel is an IBAction, IBAction is the same thing as void so it is callable } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { NSLog(@"alertView1"); } if (buttonIndex == 0) { NSLog(@"alertView2"); } } @end 

BookmarksTableViewController.h

  @interface BookmarksTableViewController : UIViewController { id delegate; } @property (nonatomic,strong) id delegate; @end 

BookmarksTableViewController.m

 - (IBAction)goBack { [self.delegate dismissVCAndEndBookmark]; } 

特别是,如果我正确理解你的意图,那么BookmarksTableViewController.m中的callback似乎是你实现中的主要问题。

 [self.navigationController popViewControllerAnimated:YES]; 

也为我做了诡计。 在我的情况下,我有一个(iPhone)viewController被push到堆栈使用push segue。 由于启动segue的viewController有一个导航栏,我必须发送父控制器的navigationController的popViewControllerAnimated消息,而不是调用它自己的dismissViewControllerAnimated:完成消息。

射线

第一视图:

首先在第一个视图中embedded导航控制器。 并使用此代码导航到另一个视图

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; FirstView *rvc = [storyboard instantiateViewControllerWithIdentifier:@"apps"]; [self.navigationController pushViewController:rvc animated:YES]; 

第二种观点:

在dismiss视图的方法中添加此行

 - (IBAction)back{ [self.navigationController popViewControllerAnimated:YES]; }