2016-07-06 36 views
0

我是Objective-C的新手。我有一個表格視圖和一個網址列表。當用戶點擊表格視圖中的單元格時,它們將被引導至具有顯示網站的網頁視圖的第二個視圖控制器。但是,當我運行該應用程序時,Web視圖顯示爲空白。我認爲didSelectRowAtIndexPath是問題。那麼如何將URL傳遞給第二個視圖控制器?didSelectRowAtIndexPath不傳遞url到下一個視圖控制器?

ListTableViewController.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    FeaturedWebViewController *vc = [[FeaturedWebViewController alloc] init]; 
    vc.url = cell.textLabel.text; 

    [self.navigationController pushViewController:vc animated:YES]; 
} 

FeaturedWebViewController.m

NSURL *URL = [NSURL URLWithString: [self.url stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 

NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
[self.webView loadRequest:request]; 
+0

我會仔細檢查cell.textLabel.text是否具有正確的值;也許在代碼中停下來,並確保vc.url包含你期望的字符串。如果看起來不錯,那麼問題可能在FeaturedWebViewController中。也許從String到NSURL的轉換失敗了?不確定...如果您仍然遇到問題,請發佈代碼。 –

+0

我們還需要在使用'.url'值的地方看到第二個視圖控制器的代碼。 – Putz1103

+0

我不會從單元拉取數據,從您填充單元格的結構中獲取數據。 –

回答

0

也許你cell.textLabel.text是零,因爲WMios一小時前說。

你應該得到原始的數據數組,它可以使你的tableview流行起來。單元格內容在已經顯示時被刷新。所以當所有的單元格都被重用時,你不能得到當前顯示的cell.textlabel.text;

所以,如果你使用coredata或popluated可變數組(=低於listOfItem),那麼這將是這樣的:

YourCoreDataSet *yourDataRow = [listOfItem objectAtIndex:indexPath.row]; 
FeaturedWebViewController *vc = [[FeaturedWebViewController alloc] init]; 
vc.url = yourDataRow.yourDataFieldHere; 

//我建議你使用的NSLog檢查這樣的:

NSLog(@"sending value: %@, received value: %@", yourDataRow.yourDataFieldHere, vc.url); 
0

幾個斷點應該可以解決這個問題,但假設textLabel.text具有正確的url值,並且它是一個有效的url(不包含空格),那麼問題可能是您的[self.webView loadRequest:request]在您設置之前正在運行FeaturedWebViewController的url屬性。

沒有任何更多的信息,儘管這都是猜測。使用調試器並確定您是否在[NSURLRequest requestWithURL:URL]中創建非零NSURL對象。

0

步驟1: - 在FirstVC,.m文件中記下此代碼。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

return mutArryAllUrl.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath]; 
cell.lblUrl.text = mutArryAllUrl[indexPath.row]; 
return cell; 
} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

SecondVC *aSecondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"]; 

//Here create one NSString Property in SecondVC, name is 'strURL' for getting URL. 
SecondVC.strURL = mutArryAllUrl[indexPath.row]; 
[self.navigationController pushViewController:aSecondVC withDefinedAnimated:YES]; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

步驟2: - 寫下該代碼在SecondVC,h文件

@property (strong, nonatomic) IBOutlet NSString *strURL; 
@property (weak, nonatomic) IBOutlet UIWebView *webView; 

步驟3: - 寫下該代碼在SecondVC,.m文件。 URL存儲在_strURL屬性中,並在Web視圖中加載URL。

- (void)viewDidLoad { 
[super viewDidLoad]; 
NSURL *url = [NSURL URLWithString:_strURL]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
[_webView loadRequest:urlRequest]; 
} 
相關問題