How to create a Modal View Programmatically (and dismiss it)

Discussion in 'iOS Programming' started by roeik, Aug 14, 2012.

  1. roeik, Aug 14, 2012
    Last edited by a moderator: Aug 14, 2012

    macrumors member

    Joined:
    Dec 25, 2008
    #1
    I am trying to create UIWebView programmatically that will be opened in a modal view. I am having troubles adding a button to dismiss the view. This is what I have so far on the main view:

    Code:
    CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 480);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
    [webView setBackgroundColor:[UIColor greenColor]];
    NSString *urlAddress = @"http://www.google.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
     
    UIViewController *viewController = [[UIViewController alloc] init];
    viewController.view = webView;
    
                
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    //viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
            
    [updateController presentViewController:navigationController animated:YES completion:nil];

    I am not sure how to tell the done button its job is to dismiss the modal view. Any suggestions?
     
  2. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    Depending on your version of iOS, add one of the following methods inside of your done: method. The second one is deprecated.

    Code:
    dismissViewControllerAnimated:completion:
    dismissModalViewControllerAnimated:
    
    Read about them here.

    You can also call them from within the presented view controller.
     
  3. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    I wrote a reply to this already, but it seems to be gone. Odd, that.

    You are going about this the wrong way. Don't create a generic view controller in code and add a webview to it. Create a custom subclass of UIViewController and set up it's views in IB. Let's call it myWebVC. Give it a "showModalDoneButton" When true, have it un-hide a done button that dismisses itself.

    Have the myWebVC include a web view. Also give it an NSURL property.

    In myWebVC's viewWillAppear method, if the URL property is not nil, make an URL Request from it and send it to the web view.

    Then present your custom
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Although the Apple-recommended approach is to have the presenting viewController act as delegate and have it do the dismissing, in a delegate method.
     
  5. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    Good point.
     
  6. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    If roeik wants to do this programmatically that is his choice. That is not a 'wrong way'.
     
  7. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    Except that creating a generic UIViewController and attaching objects to it in code means that you can't have methods in the view controller that actually control things.

    It WOULD be possible to have the view controller set itself up through code rather than using nib files, it's true.
     
  8. thread starter macrumors member

    Joined:
    Dec 25, 2008
    #8
    Well, I was just wondering if there is a way to pull this off programmatically since I don't want to create a xib file and I would like to reproduce the same code on different apps whenever I need it .

    The problem is I still didn't understand how I am supposed to call dismissViewControllerAnimated:completion.

    "viewController" (as named in my code) supposed to call it, right? But how do I tell the done btn to call dismissViewControllerAnimated:completion?
     
  9. Duncan C, Aug 15, 2012
    Last edited by a moderator: Aug 15, 2012

    macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    Read my post above. I explain that your view controller needs to be a custom subclass of UIViewController, not a generic UIViewController object. That way you can write IBAction methods that get triggered from buttons.

    Create a button in your view controller and write an IBAction method for that button that dismisses the view controller:

    Code:
    -(IBAction) doneButton;
    {
       [self.presentingViewController dismissViewControllerAnimated: TRUE completion: nil];
    }
     
  10. thread starter macrumors member

    Joined:
    Dec 25, 2008
    #10
    I meant if there is a way to do everything in one block of code, in other words, if I can add the doneButton-action without actually adding another class to my project...
     
  11. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #11
    Not really.

    Use a UIAlertView rather than a modal view controller. That's what UIAlertViews are for. (Displaying messages to the user and possibly getting back a selection with a button.)
     

Share This Page