2012-01-09 87 views
1

我是新來的IOS和目標C. 該場景是我有2個按鈕,它打開(與segue)2視圖控制器包含一個UIWebview。 我認爲最好用1個UIWebView來做,所以我試圖傳遞webvew的請求對象,並只使用一個webview控制器。使用prepareForSegue傳遞一個UIWebView請求

所以我得到了wwwBtn(UIButton打開一個網站)和fbBtn(UIButton去Facebook的URL)我的viewController和wwwWebViewController其中包含th UIWebView。

這是我做到的。

的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 *[email protected]"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]; 
    } 
+0

我知道這是一個非常古老的問題,但如果我的代碼幫助你,如果你接受答案會很高興。 – Ashoor 2013-10-29 08:11:59

回答

4

,如下面的,你將這個類指派至您需要將的NSString* urlstr傳遞給他們的目的地UIViewControllers:

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

} 

現在是時候從我的ma創建兩個segues在故事板類InfoViewController到目標類WebViewController,在我的情況下,我創建了三個segues,但只有兩個正在使用WebViewController類。

enter image description here

也已經給予塞格斯爲每一個空間標識符以編程方式對付它們的主類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 *[email protected]"http://www.google.com"; 
     webController = [segue destinationViewController]; 
     webController.urlstr = urlstr; 
    } else if ([[segue identifier] isEqualToString:@"FTAPush"]) { 
     NSString *[email protected]"http://www.yahoo.com"; 
     webController = [segue destinationViewController]; 
     webController.urlstr = urlstr; 
    } 
} 

@end 

我希望這將解決你的問題,如果你有任何問題,請不要猶豫,問。

+0

在我的情況下,它是一個_UITableViewCell_,它將把用戶帶到目標UIViewController,在你的情況中你只有Buttons,並且你將爲每一個創建一個'IBAction'並且只爲'performSegueWithIdentifier'創建一個if語句,直截了當。 – Ashoor 2012-12-05 13:01:50