我是IOS和Objective C的新手。 场景是我有2个按钮,打开(带有segue)2个包含UIWebview的视图控制器。 我认为最好用1 UIWebView来做,所以我试图传递webvew的请求对象并只使用一个webview控制器。
所以我得到了wwwBtn(打开网站的UIButton)和fbBtn(转到Facebook网址的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;
}
}
@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];
}
答案 0 :(得分:4)
执行以下操作,您将此类分配给目标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创建两个segue到目标类WebViewController,在我的例子中我创建了三个segues但只有两个使用了WebViewController类。
您还要为segue提供一个空间标识符,以便在主类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
我希望这可以解决您的问题,如果您有任何疑问,请不要犹豫。