2016-03-29 77 views
5

所以....我有一個視圖控制器,當我按下一個按鈕,另一個視圖控制器出現:dismissViewControllerAnimated不dissmissing的ViewController

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

,但把我帶回到了幾個視圖控制器遠。

我錯過了什麼嗎?

在我的第一個視圖控制器的.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

+0

我認爲dismissViewController只會在您呈現ViewController時才起作用。如果你是推視圖控制器,那麼你需要彈出 – Arun

+0

我相信我推視圖控制器,並推動LazyPDFViewController第二個視圖控制器 – user979331

回答

1

試試這個:

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

} 

代碼: [[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil]; 只是走得太遠。

+1

您不應該需要調用presentsViewController,因爲調用無論如何都會找到它。 Apple表示:「呈現視圖控制器負責解除它所呈現的視圖控制器。如果您在呈現的視圖控制器本身上調用此方法,則UIKit會要求呈現視圖控制器處理解除視圖。」 [developer.apple.com](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index。HTML#// apple_ref/OCC/instm/UIViewController中/ dismissViewControllerAnimated:完成:) – Michael

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

    } 

而在你的控制器編號2,我只是用上面的代碼:

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

對我來說一切工作正常。

+0

我很抱歉這不適合我。 – user979331

+0

我更新了問題。 – user979331

+0

如果你不能重現這個問題,你應該在「這個問題是由一個無法複製的問題引起的」原因下標記這篇文章。不要發佈回答「我無法重現您的問題」。 – JAL

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]; 
    }); 
} 
+0

我試過你的代碼,它沒有工作....相同的結果。 – user979331

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]; 
    } 
} 
相關問題