【问题标题】:Pass a UIWebView request using prepareForSegue使用 prepareForSegue 传递 UIWebView 请求
【发布时间】:2012-02-06 04:05:45
【问题描述】:

我是 IOS 和 Objective C 的新手。 场景是我有 2 个按钮,它们打开(使用 segue)2 个包含 UIWebview 的视图控制器。 我认为使用 1 个 UIWebView 会更好,所以我尝试传递 webvew 的请求对象并仅使用一个 webview 控制器。

所以我得到了 wwwBtn(打开网站的 UIButton)和 fbBtn(转到 Facebook URL 的 UIButton)我的 viewController 和包含 UIWebView 的 wwwWebViewController。

这就是我的做法。

viewController.h 文件:

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

ViewController.m 文件:

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

wwwWebViewController.h 文件:

@interface wwwWebViewController : UIViewController {
    UIWebView *webView;
}

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

wwwWebViewController.m 文件:

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

【问题讨论】:

  • 我知道这是一个非常古老的问题,但如果我的代码对您有所帮助,如果您接受答案将很高兴。

标签: uiwebview ios5 segue


【解决方案1】:

执行以下操作,您将把这个类分配给目标 UIViewControllers,您需要将 NSString *urlstr 传递给它们:

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

}

现在是时候从我的主故事板类 InfoViewController 到目标类 WebViewController 创建两个 segue,在我的例子中,我创建了三个 segue,但只有两个使用 WebViewController 类。

您还必须为每个 segues 提供一个空间标识符,以便在主类 InfoViewController 中以编程方式定位它们。

让我们看看 InfoViewController : UITableViewController 的样子:

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

我希望这能解决您的问题,如果您有任何问题,请随时提问。

【讨论】:

  • 在我的情况下,它是一个 UITableViewCell,它将把用户带到目标 UIViewController,在你的情况下,你只有按钮要容易得多,你将创建一个 @ 987654325@ 代表每个 performSegueWithIdentifier,没有 if 语句,直截了当。
猜你喜欢
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 2015-09-18
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
相关资源
最近更新 更多