dismissViewControllerAnimated未能解除视图控制器。

5

所以......我有一个视图控制器,当我按下一个按钮时,另一个视图控制器出现:

- (IBAction)searchButtonPressed:(id)sender {
    [self presentViewController:self.controllerSearch animated:YES completion:nil];
}

第二个视图控制器中有一个表格视图,当在表格中选择一行时会运行以下代码:

NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)

        NSString *filePath2 = filePath; assert(filePath2 != nil); // Path to first PDF file

        LazyPDFDocument *document = [LazyPDFDocument withDocumentFilePath:filePath2 password:phrase];

        if (document != nil) // Must have a valid LazyPDFDocument object in order to proceed with things
        {
            LazyPDFViewController *lazyPDFViewController = [[LazyPDFViewController alloc] initWithLazyPDFDocument:document];

            lazyPDFViewController.delegate = self; // Set the LazyPDFViewController delegate to self

#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)

            [self.navigationController pushViewController:lazyPDFViewController animated:YES];

#else // present in a modal view controller

            lazyPDFViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            lazyPDFViewController.modalPresentationStyle = UIModalPresentationFullScreen;

            [self presentViewController:lazyPDFViewController animated:YES completion:NULL];

#endif // DEMO_VIEW_CONTROLLER_PUSH
        }
        else // Log an error so that we know that something went wrong
        {
            NSLog(@"%s [LazyPDFDocument withDocumentFilePath:'%@' password:'%@'] failed.", __FUNCTION__, filePath2, phrase);
        }

现在我正在使用LazyPDFKit,它带有以下代理方法:
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    // dismiss the modal view controller
    [self dismissViewControllerAnimated:YES completion:NULL];

}

我设了一个断点,发现代码进入了委托方法,但是LazyPDFViewController并没有消失。

我尝试了以下方法:

[[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];

但这使我回到了过于远的几个视图控制器。

我是否遗漏了什么?

我的第一个视图控制器.h文件中有额外的代码。

@property (strong, nonatomic) UISearchController *controllerSearch;

在第一个视图控制器中的 .m 文件中。

- (UISearchController *)controller {

    if (!_controllerSearch) {

        // instantiate search results table view
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
        LHFileBrowserSearch *resultsController = [storyboard instantiateViewControllerWithIdentifier:@"SearchResults"];

        // create search controller
        _controllerSearch = [[UISearchController alloc]initWithSearchResultsController:resultsController];
        _controllerSearch.searchResultsUpdater = self;

        // optional: set the search controller delegate
        _controllerSearch.delegate = self;

    }
    return _controllerSearch;
}

1
你是通过什么方式进入LazyPDFViewController的?是将视图控制器推到当前视图控制器上,还是以模态形式呈现视图控制器? - Arun
我认为dismissViewController只在你是presentingViewController时才有效。如果你正在push视图控制器,则需要pop回去。 - Arun
相同的结果,不做任何事情。 - user979331
@user979331 你需要添加以下代码来添加子视图控制器和移除子视图控制器,如下所示://添加子视图控制器 [self addChildViewController:aVC]; [self.view addSubview:aVC.view]; [aVC didMoveToParentViewController:self]; //移除子视图控制器 [self removeFromParentViewController]; [self didMoveToParentViewController:nil]; [self.view removeFromSuperview]; - Vvk
@vvk 你的代码有点可行,但是有一些元素 childVC 没有覆盖到,比如导航栏。 - user979331
显示剩余7条评论
5个回答

2
如果您正在推送视图控制器:
[self.navigationController pushViewController:lazyPDFViewController animated:YES];

那么代理中的代码就没有意义了,因为它假设它是一个需要被解除的模态视图控制器:

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    // dismiss the modal view controller
    [self dismissViewControllerAnimated:YES completion:NULL];

}

但是你把它加入了导航堆栈中(我假设)。

如果此时无法从导航控制器中弹出它,那么你的示例代码中可能缺少一些代码。

你确定你的委托正在主线程上触发吗?尝试:

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}

1

try this:

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    // dismiss the modal view controller
    [[viewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];

}

你的代码: [[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil]; 走得太远了。


1
您不应该需要调用 presentingViewController,因为该调用会自动找到它。Apple 表示:“显示视图控制器需要负责解除其呈现的视图控制器。如果您在呈现的视图控制器本身上调用此方法,则 UIKit 会要求呈现视图控制器处理解除操作。” developer.apple.com - Michael

0

看起来你需要为展示和取消使用相同的宏。所以,你写了:

#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)

            [self.navigationController pushViewController:lazyPDFViewController animated:YES];

#else // present in a modal view controller

            lazyPDFViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            lazyPDFViewController.modalPresentationStyle = UIModalPresentationFullScreen;

            [self presentViewController:lazyPDFViewController animated:YES completion:NULL];

#endif // DEMO_VIEW_CONTROLLER_PUSH

因此,您需要

#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)

        [self.navigationController popViewControllerAnimated:YES];

    #else // presented in a modal view controller

                [self dismissViewControllerAnimated:YES completion:NULL];

    #endif // DEMO_VIEW_CONTROLLER_PUSH

有可能您已经关闭了主线程,您可以始终添加一个断言进行检查,或者如建议的那样使用dispatch_async来确保。

NSAssert([NSThread isMainThread)];

当我知道代码中所有流程时,我更喜欢使用断言,因为它向未来的自己(或其他人)展示了我的假设,并且不会留下看起来像是它知道我不知道的东西的代码(哦,他们正在将dispatch_async用于主线程,所以在更深层次上必须有一些其他的线程魔法发生)。


0
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    if (![NSThread isMainThread])
    {
        dispatch_async(dispatch_get_main_queue(), ^
                       {
                           [self dismissLazyPDFViewController:viewController];
                       });
        return;
    }
    if (viewController.navigationController)
    {
        [viewController.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [viewController dismissViewControllerAnimated:YES completion:nil];
    }
}

0

我根据你的情况刚刚制作了演示项目。我没有遇到任何问题,所以我认为可能是你如何呈现第二个控制器方面出了一些问题。

在你的按钮点击事件中,尝试使用这段代码:

- (IBAction)searchButtonPressed:(id)sender {
          UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
         //idSecondVC is the storyboard id of second view controller
        SecondVC *SecondVC = [main instantiateViewControllerWithIdentifier:@"idSecondVC"];
       [self presentViewController:SecondVC animated:YES completion:nil];

    }

在您的第二个控制器中,我只是使用了上述代码:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString  *cellIdentifier = @"Cell";
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.textLabel.text = [NSString stringWithFormat:@"Cell %ld",indexPath.row];
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self openLazyPDF];
}

- (void)openLazyPDF
{
    NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)

    NSArray *pdfs = [[NSBundle mainBundle] pathsForResourcesOfType:@"pdf" inDirectory:nil];

    NSString *filePath = [pdfs firstObject]; assert(filePath != nil); // Path to first PDF file

    LazyPDFDocument *document = [LazyPDFDocument withDocumentFilePath:filePath password:phrase];

    if (document != nil) // Must have a valid LazyPDFDocument object in order to proceed with things
    {
        LazyPDFViewController *lazyPDFViewController = [[LazyPDFViewController alloc] initWithLazyPDFDocument:document];

        lazyPDFViewController.delegate = self; // Set the LazyPDFViewController delegate to self

#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)

        [self.navigationController pushViewController:lazyPDFViewController animated:YES];

#else // present in a modal view controller

        lazyPDFViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        lazyPDFViewController.modalPresentationStyle = UIModalPresentationFullScreen;

        [self presentViewController:lazyPDFViewController animated:YES completion:NULL];

#endif // DEMO_VIEW_CONTROLLER_PUSH
    }
    else // Log an error so that we know that something went wrong
    {
        NSLog(@"%s [LazyPDFDocument withDocumentFilePath:'%@' password:'%@'] failed.", __FUNCTION__, filePath, phrase);
    }
}

#pragma mark - LazyPDFViewControllerDelegate methods

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    // dismiss the modal view controller
    [self dismissViewControllerAnimated:YES completion:NULL];
}

对我来说,一切都正常运行。


如果您无法重现此问题,您应该在此帖子下标记关闭原因为“此问题是由于无法再现的问题引起的”。请勿发布回答说“我无法重现您的问题”。 - JAL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接