views:

247

answers:

1

I have a Navigation Controller app and i'm using this code to switch views:

-    (IBAction)switchPage:(id)sender
    {
    if(self.fourthViewController == nil)
    {
        FourthViewController *fourthView = [[FourthViewController alloc]
                                              initWithNibName:@"FourthView" bundle:[NSBundle mainBundle]];
        self.fourthViewController = fourthView;
        [fourthView release];
    }

            [self.navigationController pushViewController:self.fourthViewController    animated:YES];
    }

However I want to have a button that can go to an alternate view that is a UIWebview, My goal is to not leave the app so when user is done on the web page, the navigation controller never goes away and they can proceed to the next view. Soes anyone know of a tutorial that can help? I started with this (http://www.iphonesdkarticles.com/2008/08/uiwebview-tutorial.html) but it is a standalone app/tutorial and I'm having trouble integrating it into a Nav based app.

Thanks

A: 

Why not use a modal view? Here's an example from one of my apps. So whats going on below is that I assign a credits bar button item on the nav controller nav bar which when clicked brings up the modal view controller which presents a webview. Really rather simple:

UIBarButtonItem *credits = [ [ [ UIBarButtonItem alloc ] 
           initWithTitle:@"More"
           //  initWithCustomView:testLabel
           style: UIBarButtonItemStylePlain
           target: self 
           action:@selector(credits) 
           ]
         autorelease ];

  self.navigationItem.rightBarButtonItem = credits;


-(void)credits {
 if (self.whyModalViewController == nil)
        self.whyModalViewController = [[[ModalViewController alloc] initWithAppDelegate:self ] autorelease];


 NSString *html = [ [ NSString alloc ] initWithFormat:@"%@", self.explain];

 NSString *imagePath = [[NSBundle mainBundle] resourcePath];
 NSLog(@"imagePath = %@ ",imagePath);
 imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
 imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];


 [self.whyModalViewController.webView loadHTMLString:html baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];




 [self presentModalViewController:whyModalViewController animated:YES];


 [html release];
}

There are many ways to get back to the main view. I implemented a "back" button on the webview which dismisses the modal view controller:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self action:@selector(dismissAction:) forControlEvents:UIControlEventTouchUpInside];
        NSString *title = @"<- Back";
        button.backgroundColor = [UIColor clearColor];
        [button setTitle: title forState:UIControlStateNormal];

- (IBAction)dismissAction:(id)sender
{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}
ennuikiller