iPhone: Show modal UITableViewController with Navigation bar

前端 未结 6 1045
温柔的废话
温柔的废话 2021-01-30 00:10

I am showing a modal view which is a UITableViewController class. For some reason it won\'t show the navigation bar when I show it. Here is my code:



        
6条回答
  •  一个人的身影
    2021-01-30 00:25

    I just wanted to add something to what @Scott said. His answer is definitely the easiest and most accepted way of doing it now with Storyboards, iOS 7 and 8... (and soon, 9).

    Definitely adding a view controller to the Storyboard and Embedding it as described by @Scott is the right way to go.

    Then, just add the segue by control-dragging from the source view controller to the target (the one you want to show modally), select "Present Modally" when the little view appears with the choices for the type of segue. Probably good to give it a name too (in the example below I use "presentMyModalViewController").

    One thing that I needed that was missing is @Scott's case is when you want to actually pass on some data to that modally-presented view controller that is embedded in the navigation controller.

    If you grab the segue.destinationViewController, it will be a UINavigationController, not the controller you embedded in the UINavigationController.

    So, to get at the embedded view controller inside the navigation controller, here's what I did:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"presentMyModalViewController"]) {
            // This could be collapsed, but it's a little easier to see
            // what's going on written out this way.
    
            // First get the destination view controller, which will be a UINavigationController
            UINavigationController *nvc = (UINavigationController *)segue.destinationViewController;
    
            // To get the view controller we're interested in, grab the navigation controller's "topViewController" property
            MyModalViewController *vc = (EmailReceiptViewController *)[nvc topViewController];
    
            // Now that we have the reference to our view controller, we can set its properties here:
            vc.myAwesomeProperty = @"awesome!";
        }
    }
    

    Hope this helps!

提交回复
热议问题