Pass a UIWebView request using prepareForSegue

我与影子孤独终老i 提交于 2019-12-30 05:29:10

问题


I 'm new to IOS and Objective C. The scenario is I have 2 buttons, which open (with segue) 2 view controllers containing a UIWebview. I thought is better to do it with 1 UIWebView so I tried to pass the request object of the webvew and use only one webview controller.

so I got the wwwBtn (UIButton that opens a site) and fbBtn (UIButton that goes to a Facebook URL) my viewController and the wwwWebViewController which contains th UIWebView.

Here is how I did it.

The viewController.h file :

@interface ViewController : UIViewController {
    UIButton *wwwBtn;
    UIButton *fbButton;
}
@property (retain, nonatomic) IBOutlet UIButton *wwwBtn;
@property (retain, nonatomic) IBOutlet UIButton *fbBtn;

The ViewController.m file :

#import "ViewController.h"
#import "wwwWebViewController.h"

@implementation ViewController

@synthesize wwwBtn;
@synthesize fbBtn;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{        
    if ([[segue identifier] isEqualToString:@"web"]) {
            NSString *urlstr=@"http ://www.google.com";
            wwwWebViewController *vc = [segue destinationViewController];
            vc.urlStr = urlstr;
        } else if ([[segue identifier] isEqualToString:@"fb"]) { 
            NSString *urlstr = @"http://www.facebook.com";
            wwwWebViewController *vc = [segue destinationViewController];
            vc.urlStr = urlstr;
        }
    }

the wwwWebViewController.h file :

@interface wwwWebViewController : UIViewController {
    UIWebView *webView;
}

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

The wwwWebViewController.m file :

    - (void)viewDidLoad
   {
        NSURL *url = [NSURL URLWithString:urlStr];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webView loadRequest:requestObj];
        [super viewDidLoad];
    }

回答1:


Do as the following, you will assign this class to the destination UIViewControllers that you will need to pass the NSString *urlstr to them:

WebViewController.h

@interface WebViewController : UIViewController {
    NSString *urlstr;
}

@property (strong, nonatomic) IBOutlet UIWebView *WebView;
@property (strong, nonatomic) NSString *urlstr;

WebViewController.m

@implementation WebViewController

@synthesize WebView;
@synthesize urlstr;

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

    NSURL *url = [NSURL URLWithString:urlstr];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.WebView loadRequest:requestObj];

}

Now it's time to create two segues from my main storyboard class InfoViewController to the destination class WebViewController, in my case I've created three segues but only two are using the WebViewController class.

You've also to give the segues a spacial Identifier for each to target them programmatically in the main class InfoViewController.

let's check out how the InfoViewController : UITableViewController will look like:

InfoViewController.m

#import "InfoViewController.h"
#import "WebViewController.h"

@interface InfoViewController ()

@end

@implementation InfoViewController

@synthesize AboutCell = _AboutCell;
@synthesize CGCell = _CGCell;
@synthesize FTACell = _FTACell;

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *AboutCellClicked = [self.tableView cellForRowAtIndexPath:indexPath];
    UITableViewCell *CGCellClicked = [self.tableView cellForRowAtIndexPath:indexPath];
    UITableViewCell *FTACellClicked = [self.tableView cellForRowAtIndexPath:indexPath];

    if (AboutCellClicked == _AboutCell) {
        [self performSegueWithIdentifier:@"AboutPush" sender:self];
    } else if (CGCellClicked == _CGCell) {
        [self performSegueWithIdentifier:@"CGPush" sender:self];
    } else if (FTACellClicked == _FTACell) {
        [self performSegueWithIdentifier:@"FTAPush" sender:self];
    }

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    WebViewController *webController = [[WebViewController alloc] init];

    if ([[segue identifier] isEqualToString:@"CGPush"]) {
        NSString *urlstr=@"http://www.google.com";
        webController = [segue destinationViewController];
        webController.urlstr = urlstr;
    } else if ([[segue identifier] isEqualToString:@"FTAPush"]) {
        NSString *urlstr=@"http://www.yahoo.com";
        webController = [segue destinationViewController];
        webController.urlstr = urlstr;
    }
}

@end

I hope this will solve your problem, If you have any question do not hesitate to ask.



来源:https://stackoverflow.com/questions/8791517/pass-a-uiwebview-request-using-prepareforsegue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!