简体   繁体   中英

Using Navigation Controller Template and a getting a button to load a UIwebview without leaving app

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.

Does anyone know of a tutorial that can help?

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM