Swift – Using popViewController and passing data to the ViewController you're returning to

后端 未结 4 1416
孤街浪徒
孤街浪徒 2021-01-12 03:45

I have an optional bool variable called showSettings on my first view controller which is called ViewController, and I\'m popping from

4条回答
  •  不知归路
    2021-01-12 04:22

    I came across this while looking for a way to do it. Since I use Storyboards more often, I found that I can get the array of controllers in the navigation stack, get the one just before the current one that's on top, check to see if it's my delegate, and if so, cast it as the delegate, set my methods, then pop myself from the stack. Although the code is in ObjC, it should be easily translatable to swift:

    // we need to get the previous view controller
    NSArray *array = self.navigationController.viewControllers;
    if ( array.count > 1) {
        UIViewController *controller = [array objectAtIndex:(array.count - 2)];
        if ( [controller conformsToProtocol:@protocol(GenreSelectionDelegate)]) {
            id genreDelegate = (id)controller;
            [genreDelegate setGenre:_selectedGenre];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    

提交回复
热议问题