首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell与url连接到UIViewController与WebView?

UITableViewCell与url连接到UIViewController与WebView?
EN

Stack Overflow用户
提问于 2015-12-29 23:23:29
回答 2查看 64关注 0票数 0

长话短说,我一整天都在与这段代码作斗争,我使用的是prepareForSegue方法。我有一个随机链接,用户输入并放置在一个tableView中。现在,用户可以单击tableView中的单元格,该单元格通过另一个带有WebView的UIViewController连接到链接。问题是我无法获得在webView中显示网站的算法。我对目标C也很陌生。

代码语言:javascript
复制
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showArticle"]) {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *string = [self.toDoItems objectAtIndex:indexPath.row];
    [[segue destinationViewController] setUrl:string];

这是来自tableView的。也许我应该使用didSelectRowAtIndexPath帮助!

EN

回答 2

Stack Overflow用户

发布于 2015-12-29 23:42:30

你需要使用didSelectRowAtIndexPath:!否则,您的TableViewController不知道用户选择了一个单元格。

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.propertyToStoreTheString = [self.toDoItems objectAtIndex:indexPath.row];
   [self performSegueWithIdentifier:@"showArticle" sender:nil];
}

prepareForSegue-method中,您只需使用[((MyViewController *)[segue destinationViewController]) setUrl:self.propertyToStoreTheString];

票数 0
EN

Stack Overflow用户

发布于 2015-12-29 23:55:02

有一个简单的情况,这就是我所做的://在包含webView的类中

代码语言:javascript
复制
-(BOOL) validateURL: (NSString *) candidate {
    NSString *urlRegEx =@"((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.?[\\w\\d\\-_]+\\.\\w{2,4}(\\.\\w{2})?(.*)?"; ///(?<=/)(?:[\\w\\d\\-./_]+)?
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
    return [urlTest evaluateWithObject:candidate];
  }

- (NSURL *)goToWebPage {
    NSString *fullURL;
    if(self.adressBar.text == nil){ //full text of the passed url is not nill
        return nil;
    }else if([self validateURL:self.adressBar.text]){
 #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
    if(([self.adressBar.text rangeOfString:@"http://"].location != NSNotFound) || ([self.adressBar.text rangeOfString:@"https://"].location != NSNotFound)){
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000
    if([self.adressBar.text containsString:@"http://"] || [self.adressBar.text containsString:@"https://"]){
#endif
    fullURL = self.adressBar.text;
    }else {
        fullURL = [NSString stringWithFormat:@"http://%@",self.adressBar.text];
    }
    }else {
        NSString *new = [self.adressBar.text stringByReplacingOccurrencesOfString: @" " withString:@"%20"];
        fullURL = [NSString stringWithFormat:@"https://www.google.com/search?q=%@",new];
    }
    NSURL *url = [[NSURL alloc] initWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

        [_myWebView loadRequest:requestObj];
        _myWebView.scalesPageToFit = true;
    return url;
}

我在我的第一个iOS应用程序中使用了这段代码,所以我对它并不感到骄傲,但它确实对我有用。

关于传递URL,您可以这样做,至少我有相同的代码:

代码语言:javascript
复制
BrowserPage * destinatinatioonViewController = segue.destinationViewController;
    destinatinatioonViewController.adressBar.text= [self.adressBar text];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34520674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档