2012-03-19 41 views
0

我在修改一些我一直留下來的代碼,並希望爲此應用程序添加一些funcationality。我有一個表視圖和didSelectRowAtIndexPath事件,我只是加載一個具有UIWebView的控制器。就像這樣:將URL傳遞給具有UIWebView的UIViewController的最簡單方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     WebViewController *webViewController = [[WebViewController alloc] 
               initWithNibName:@"WebViewController" bundle:nil]; 

我真正想要做的是通過在URL中加載正如我剛纔已經硬編碼的URL加載viewDidLoad中是這樣的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = NSLocalizedString(@"WebTitle", @""); 
    CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; 
    webFrame.origin.y=0; 
    self.myWebView = [[UIWebView alloc] initWithFrame:webFrame]; 
    self.myWebView.backgroundColor = [UIColor whiteColor]; 
    self.myWebView.scalesPageToFit = YES; 
    self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
    self.myWebView.delegate = self; 
    [self.view addSubview:self.myWebView]; 
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com/"]]]; 

什麼是最簡單的方法在WebViewController的實例化中傳遞stackoverflow.com的url?

THX

回答

0

使.h文件中的屬性URL,那麼財產指定爲

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     WebViewController *webViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil]; 
     webViewController.url = [NSURL URLWithString:@"http://stackoverflow.com/"]; 
} 
+0

並添加屬性'@屬性(非原子,保留)NSURL * URL;'到WebViewController? – timpone 2012-03-19 14:44:24

+0

是的,這就是你如何聲明屬性.. – 2012-03-19 15:57:50

相關問題