PDA

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




roeik
Aug 14, 2012, 01:52 PM
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:

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?



xStep
Aug 14, 2012, 11:45 PM
Depending on your version of iOS, add one of the following methods inside of your done: method. The second one is deprecated.


dismissViewControllerAnimated:completion:
dismissModalViewControllerAnimated:


Read about them here (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html).

You can also call them from within the presented view controller.

Duncan C
Aug 15, 2012, 08:58 AM
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:

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?

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

dejo
Aug 15, 2012, 09:51 AM
When true, have it un-hide a done button that dismisses itself.

Although the Apple-recommended approach is to have the presenting viewController act as delegate and have it do the dismissing, in a delegate method.

Duncan C
Aug 15, 2012, 10:05 AM
Although the Apple-recommended approach is to have the presenting viewController act as delegate and have it do the dismissing, in a delegate method.

Good point.

xStep
Aug 15, 2012, 01:00 PM
You are going about this the wrong way.

If roeik wants to do this programmatically that is his choice. That is not a 'wrong way'.

Duncan C
Aug 15, 2012, 01:14 PM
If roeik wants to do this programmatically that is his choice. That is not a 'wrong way'.

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.

roeik
Aug 15, 2012, 01:35 PM
If roeik wants to do this programmatically that is his choice. That is not a 'wrong way'.

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?

Duncan C
Aug 15, 2012, 01:45 PM
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?

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:

-(IBAction) doneButton;
{
[self.presentingViewController dismissViewControllerAnimated: TRUE completion: nil];
}

roeik
Aug 15, 2012, 02:19 PM
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:

-(IBAction) doneButton;
{
[self.presentingViewController dismissViewControllerAnimated: TRUE completion: nil];
}

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...

Duncan C
Aug 15, 2012, 02:36 PM
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...

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.)