dismissViewController: not working

前端 未结 3 1569
梦谈多话
梦谈多话 2021-02-04 18:44

I have a view controller called vc0 which is presented like this:

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

And in vc1 I

相关标签:
3条回答
  • 2021-02-04 19:02

    I would recommend to always dismiss a VC from the VC that actually presented it - using a delegate. This is actually also the Apple recommended way - as was pointed out in a previous answer to my question regarding this issue.

    So if you have VC0 presenting VC1, have the dismiss VC1 code in VC0 too, using delegate scheme.

    I have learned that this is the savest way to handle presenting and dismissing - even though sometimes it works to dismiss VC1 within VC1 itself.

    I have asked a very related question, you might be interested to check this too. It shows the code...

    ps I also read that some dismiss only VC1 - which in turn will also dismiss VC2. However, if my previous suggestion works, I would not do this. Sometimes I get information during execution (no crash) that the VC does not exist anymore or anything related to that - so I assumed this is not the best solution. But if my prvious suggestion does not work, you may try the second one.

    Though no guarantee that this will last the updates to new iOS, since this issue keeps hauting me for several iOS updates now :-), so I decided to go the standard recommended route.

    EDIT: This is my modified code - it works without problems - however it is not clear what you intend to happen AFTER the user reponds to the Alert and whether the Alert should be on VC1 or VC0. Anyway using delegate and callbacks I do not see any issue. Please explain should I have missed your point...

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

    Esp the callback in BookmarksTableViewController.m seems to be the main issue in your implementation if I understood your intentions correctly.

    0 讨论(0)
  • 2021-02-04 19:14
    [self.navigationController popViewControllerAnimated:YES];
    

    did the trick for me also. In my case, I had a (iPhone) viewController that was pushed on to the stack using a push segue. Since the viewController that was initiating the segue had a navigation bar, I had to send the parent controller's navigationController the popViewControllerAnimated message instead of calling it's own dismissViewControllerAnimated:completion message.

    Ray

    0 讨论(0)
  • 2021-02-04 19:27

    First View:

    Firstly embed in navigation controller in first view. and use this code to navigate to another view

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

    Second view:

    Add this line in the method for dismiss view

    - (IBAction)back{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    0 讨论(0)
提交回复
热议问题