Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

roeik

macrumors member
Original poster
Dec 25, 2008
80
0
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?
 
Last edited by a moderator:
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.
 
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?

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

Code:
-(IBAction) doneButton;
{
   [self.presentingViewController dismissViewControllerAnimated: TRUE completion: nil];
}
 
Last edited by a moderator:
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];
}

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...
 
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.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.