Create a modal view with navigation bar and back button

后端 未结 2 1232
别跟我提以往
别跟我提以往 2020-12-29 16:29

I want to create a modal view with the navigation item (right view on my screenshot) and I want it to have a \'back button\'. My app is TabBar application and I don\'t want

2条回答
  •  一生所求
    2020-12-29 16:50

    Just put a Navbar on the new view with a bar button item. Create an action for the bar button item by control dragging from the bar button item to the .h of the view controller. You can then use a delegate and protocol method to tell the first controller when the button has been pressed and have it use [self dismissModalViewControllerAnimated:YES];

    So in your second view create a protocol with a method done, like this:

    @protocol SecondViewControllerDelegate 
    
    -(void) done;
    
    @end
    
    @interface SecondViewController : UIViewController {
        ...
        id delegate;
    }
    
    ...
    
    @property (nonatomic, assign) id delegate;
    
    -(IBAction)done:(id)sender;  //this will be linked to your nav bar button.
    @end
    

    then in your action from your button call this:

    -(IBAction)done:(id)sender{
       [self.delegate done];
    }
    

    Your first view controller will need to implement the protocol

    then in your first view controller, set it up as a delegate for your second view controller before you segue.

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if([[segue identifier] isEqualToString:@"Second View Modal Segue Identifier"])
        {
            SecondViewController *viewController = segue.destinationViewController;
            viewController.delegate = self;
        }
    }
    

    lastly, catch the done call from the delegate in your first view controller:

    -(void) done
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    

    That's how I have done it. If you don't have a lot of experience with protocols and delegates it may seem confusing at first, but it has worked well for me.

提交回复
热议问题