使用导航控制器模板和获取按钮来加载UIwebview而不需要离开app

时间:2022-09-24 19:44:35

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

我有一个导航控制器app,我用这个代码切换视图:

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

但是我想要一个按钮可以进入另一个UIWebview的视图,我的目标是不离开应用所以当用户在网页上完成时,导航控制器永远不会消失他们可以进入下一个视图。

Does anyone know of a tutorial that can help?

有没有人知道一个可以帮助你的教程?

1 个解决方案

#1


0  

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:

为什么不使用模态视图呢?这是我的一个应用程序的例子。下面发生的是我在导航条上分配了一个积分栏按钮当点击它时,会弹出显示webview的模态视图控制器。很简单:

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:

有很多方法可以回到主视图。我在webview上实现了“后退”按钮,取消了模态视图控制器:

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

#1


0  

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:

为什么不使用模态视图呢?这是我的一个应用程序的例子。下面发生的是我在导航条上分配了一个积分栏按钮当点击它时,会弹出显示webview的模态视图控制器。很简单:

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:

有很多方法可以回到主视图。我在webview上实现了“后退”按钮,取消了模态视图控制器:

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