dismissViewControllerAnimated不会解散ViewController

时间:2016-03-29 17:49:14

标签: ios objective-c

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

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

内部视图控制器编号2是一个表视图,当在表中选择一行时,此代码运行:

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];

但这让我回到了几个视图控制器。

我错过了什么吗?

我的第一个视图中的附加代码Controller .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;
}

5 个答案:

答案 0 :(得分: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 :(得分:1)

试试这个:

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

}

你的代码: [[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil]; 太过分了。

答案 2 :(得分:0)

我刚刚根据你的情况制作了演示项目。而且我没有遇到任何问题。所以我认为关于你如何呈现第二个控制器可能存在一些问题。

在按钮单击中,尝试以下代码:

-(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];
}

在你的2号控制器中,我只使用了上面的代码:

{{1}}

对我来说一切都很好。

答案 3 :(得分:0)

看起来你需要使用相同的宏作为dismiss。所以,你写了

//imgView is your UIImageView
let positioning = UIAnimation.moveTo(CGPointMake(view.frame.width+imgView.frame.width/2,view.frame.height/2), duration: 0) //put the UIView on the right side of the screen
let movement = UIAnimation.moveTo(CGPointMake(-imgView.frame.width/2),view.frame.height/2), duration: 5) //moves the UIView to the left side of the screen
let animation = UIAnimation.sequence([positioning,movement]) //do them sequentially
let foreverAnimation = UIAnimation.repeatAnimationForever(animation) //repeat the sequence forever
imgView.runAnimation(foreverAnimation) //start the animation

你需要

#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

您可能已经关闭了主线程,并且您可以随时添加要检查的断言,或者如建议的那样,使用dispatch_async来确定。

#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进入main,所以必须有更深层次的其他线程魔法。)

答案 4 :(得分: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];
    }
}