在Webview中加载不同的URL

时间:2013-07-10 03:01:45

标签: ios objective-c webview

我有一个应用程序,我想按下按钮并让用户进入Web视图。但是,我希望根据推送的按钮加载特定页面。最初我想为每个按钮创建一个单独的Web视图,但这只会导致更多问题,我不会介绍。所以我的问题是:如何根据是否按下特定按钮,将不同的URL加载到Web视图中?

我考虑过使用prepareForSegue方法,但认为这可能会让事情变得复杂。任何方向都会很棒。

4 个答案:

答案 0 :(得分:4)

所以你必须在其中创建带有UIWebView的新viewController。 在newViewController上创建一个名为url的NSString属性或类似的东西

示例:

@property (retain, nonatomic) NSString *url;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

on oldViewController中的buttonTouchUpInside事件

UIStoryboard *mainStorybroad = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    newViewController *webViewController = [mainStorybroad instantiateViewControllerWithIdentifier:@"webviewIdentifier"];
webViewController.url = @"url string";

最后,在newViewController中你只需要:

[self.webView loadRequest:[NSURLRequest requestWithURL:url]];

答案 1 :(得分:1)

这是.h和.m文件的代码

在其上创建一个XIB并放置UIWebView并将其绑定到类。

对于标题文件

#import <UIKit/UIKit.h>

@interface NewWebViewController : UIViewController<UIWebViewDelegate> {
    IBOutlet UIWebView *webView;
    NSString *currentURL;
    UIActivityIndicatorView *activityView;

}
@property (nonatomic, copy) NSString *currentURL;
@property (nonatomic, retain) UIActivityIndicatorView *activityView;

@end

对于实施文件(.m)

#import "NewWebViewController.h"

@interface NewWebViewController ()

@end

@implementation NewWebViewController
@synthesize currentURL;
@synthesize activityView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    webView.frame = self.view.frame;
    [webView setScalesPageToFit:YES];
    NSURL *url = [NSURL URLWithString:self.currentURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];


    self.activityView = [[[UIActivityIndicatorView alloc] init] autorelease];
    self.activityView.tag = 1;
    self.activityView.hidesWhenStopped = YES;
    UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
    self.activityView.center = customView.center;
    [customView addSubview:self.activityView];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:customView] autorelease];
    [self.activityView startAnimating];
}

#pragma mark UIWebView Delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self.activityView startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.activityView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Error %@",[error description]);
    [self.activityView stopAnimating];
}

- (void)viewDidUnload
{

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [webView stopLoading];
    webView.delegate = nil;
    webView = nil;
    self.activityView = nil;
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    webView.frame = self.view.frame;
    return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait);
} 

- (void) dealloc {
    [currentURL release];
    [webView release];
    [super dealloc];
}

@end

这里有如何使用

- (void) openWebViewControllerWithURL:(NSString*) url {
    if (!url) {
        return;
    }
    NewWebViewController *vController = [[NewWebViewController alloc] initWithNibName:@"NewWebViewController" bundle:nil];
    vController.currentURL = url;
    [self.navigationController pushViewController:vController animated:YES];
    [vController release];   
}

点击按钮,只需传递您要加载的网址

像这样

 [self openWebViewControllerWithURL:@"http://www.google.com"];

答案 2 :(得分:1)

为什么不将IB中的按钮链接到视图控制器页面进行模态转换?然后,在新视图控制器的viewdidload中,代码应如下所示:

    NSString *fullURL = @"google.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:requestObj];

你有多少个按钮?如果它只是一对,那么我只想创建一个额外的几个视图控制器。如果它是一堆,你总是可以创建一个全局变量(我已经可以听到cringing ...)并根据推送的按钮将变量设置为所需的URL。

答案 3 :(得分:1)

  

如何根据是否按下特定按钮,将不同的URL加载到Web视图中?

识别不同按钮有两种基本方法:

  • 单独操作:您可以为每个按钮创建单独的操作方法,然后将每个按钮连接到相应的操作。

  • 单个操作:您可以为所有按钮创建单个操作,并使用按钮的某些属性来区分它们。

第一个看起来像这样:

- (IBAction)button1Action:(UIButton*)sender
{
    [self.webView loadRequest:[NSURLRequest requestWithURL:url1]];
}

- (IBAction)button2Action:(UIButton*)sender
{
    [self.webView loadRequest:[NSURLRequest requestWithURL:url2]];
}

第二个看起来像:

- (IBAction)buttonsAction:(UIButton*)sender
{
    switch (sender.tag) {
        case button1Tag:
            [self.webView loadRequest:[NSURLRequest requestWithURL:url1]];
            break;
        case button2Tag:
            [self.webView loadRequest:[NSURLRequest requestWithURL:url2]];
            break;
    }
}