0

I have an app where I would like to push a button and have the user taken to a web view. However, I would like a particular page to load depending on the button that's pushed. Originally I thought about creating a separate web view for each button, but that only caused more issues, which I won't go into. So my question is: How can I load different URL's into a web view, based on whether a particular button is pushed?

I thought about using the prepareForSegue method, but thought that might be over complicating things. Any direction would be great.

4

4 回答 4

4

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

例子:

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

关于 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]];
于 2013-07-10T03:18:54.650 回答
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];   
}

单击按钮只需传递您要加载的 URL

像这样

 [self openWebViewControllerWithURL:@"http://www.google.com"];
于 2013-07-10T03:15:02.030 回答
1

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

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

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

于 2013-07-10T03:17:22.877 回答
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;
    }
}
于 2013-07-10T03:33:34.303 回答