【问题标题】:UITableViewCell with url connects to UIViewController with WebView?带有 url 的 UITableViewCell 使用 WebView 连接到 UIViewController?
【发布时间】:2015-12-29 23:23:29
【问题描述】:

长话短说,我整天都在为这段代码苦苦挣扎,我正在使用 prepareForSegue 方法。我有一个用户输入并放置在 tableView 中的随机链接。现在用户可以单击 tableView 中的单元格,该单元格通过另一个带有 WebView 的 UIViewController 连接到链接。问题是我无法获得将在 webView 中显示网站的算法。我也是 Objective-C 的新手。

-(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 帮助!

【问题讨论】:

    标签: ios objective-c uitableview webview


    【解决方案1】:

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

    -(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,你做的很好,至少我有相同的代码:

    BrowserPage * destinatinatioonViewController = segue.destinationViewController;
        destinatinatioonViewController.adressBar.text= [self.adressBar text];
    

    【讨论】:

      【解决方案2】:

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

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

      在你的prepareForSegue-方法中,你只需要使用[((MyViewController *)[segue destinationViewController]) setUrl:self.propertyToStoreTheString];

      【讨论】:

      • 好的,我使用了 didSelectRowAtIndexPath 方法。但我仍然无法弄清楚 prepareForSegue 方法。 setUrl:self.propertyToStoreTheString 出现错误。我是objective-c的新手。感谢您的帮助!
      • 制作一个你的目标视图控制器的对象。例如MyViewController *myVC = segue.destinationViewController; 然后调用这个对象的setUrl方法。原因是如果你使用[[segue destinationViewController] setUrl:self.propertyToStoreTheString],编译器不知道你调用的是哪个对象的setUrl方法
      • @MeharoofNajeeb,谢谢。我忘记了这个。但我认为,将 destinationViewController 转换为 MyViewController 应该可以。所以你可以保存一行;-) 我把这个添加到我的答案中
      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多