UITableView 推送到 URL 帮助

发布于 2024-11-28 12:40:56 字数 602 浏览 2 评论 0原文

我正在尝试推送到新视图并将 url 发送到该视图中的 webview,但它没有收到 url 请求。 StoryLink 已提前声明,但即使我尝试请求 http://www.google.com 它也不会'甚至都不起作用,所以我知道问题是它由于某种原因不接受网址。

DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
            [newviewController.rssview loadRequest:request];
            [self.navigationController pushViewController:newviewController animated:YES];

I am trying to push to a new view and send a url to the webview in that view but it's not getting the url request. storyLink is declared earlier on, but even if I try to request http://www.google.com it doesn't even work either, so I know the problem is it's not accepting the url for some reason.

DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
            [newviewController.rssview loadRequest:request];
            [self.navigationController pushViewController:newviewController animated:YES];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-12-05 12:40:56

最好在下一个视图中为 NSURL 声明一个属性,并从父视图中设置该值。

例如,在 DetailViewController.h 文件中设置以下代码 在

    @interface DetailViewController : UIViewController
    {
            NSURL *request;
    }
    @property (nonatomic, assign) NSURL *request;

DetailViewController.m 文件中...
@合成请求;

并按如下方式修改您当前的代码...

    DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
    newviewController.request = request;
    //[newviewController.rssview loadRequest:request];
    [self.navigationController pushViewController:newviewController animated:YES];  

参考您的代码...:

以下是您必须在代码中修改的一些缺失内容,它将开始正常工作。

  1. 为 UIWebview 控件设置委托。

    self.rssview.delegate = self;
    [self.rssview loadRequest:self.request]; 
    
  2. 在 DetailViewController.h 中,修改以下行。

    @interface DetailViewController : UIViewController ;
    {
        IBOutlet UIWebView *rssview;
        NSURLRequest *请求;
    }
    @property(非原子,保留,只读)UIWebView * rssview;
    @property(非原子,分配)NSURLRequest *请求;
    @结尾
    
  3. 在 DetailViewController.m 中添加 UIWebview 的委托方法,如下所示...

    - (BOOL)webView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)请求导航类型:(UIWebViewNavigationType)navigationType
    {
        返回是;
    }
    
  4. 替换以下代码

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        int StoryIndex = [indexPath indexAtPosition: [indexPath 长度] - 1];
        NSString *storyLink = [[stories objectAtIndex:storyIndex] objectForKey:@"link"]; 
        // 清理链接 - 去掉空格、回车符和制表符...
        StoryLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
        StoryLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        StoryLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
        StoryLink = [storyLink stringByAddingPercentEscapesUsingEncoding: NSStringEncodingConversionAllowLossy];
        NSLog(@"链接:%@",storyLink);
        if(indexPath.row==0)
        {       
            DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
            NSURLRequest *请求 = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
            //[newviewController.rssview loadRequest:请求];
            newviewController.request = 请求;
            [self.navigationController PushViewController:newviewController 动画:是];
        }
        别的 
        {   
            DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
            NSURLRequest *请求 = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
            //[newviewController.rssview loadRequest:请求];
            newviewController.request = 请求;
            [self.navigationController PushViewController:newviewController 动画:是];
        }
    }
    

Better you declare a property in next view for NSURL and set the value from the parent view.

For example, set following code in your DetailViewController.h file

    @interface DetailViewController : UIViewController
    {
            NSURL *request;
    }
    @property (nonatomic, assign) NSURL *request;

In your DetailViewController.m file...
@synthesize request;

And modify your current code as below...

    DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
    newviewController.request = request;
    //[newviewController.rssview loadRequest:request];
    [self.navigationController pushViewController:newviewController animated:YES];  

In reference to your code...:

Following are some missing things that you have to modify in your code and it will start working fine.

  1. Set delegate for UIWebview control.

    self.rssview.delegate = self;
    [self.rssview loadRequest:self.request]; 
    
  2. In your DetailViewController.h, modify following lines.

    @interface DetailViewController : UIViewController <UIWebViewDelegate>
    {
        IBOutlet UIWebView *rssview;
        NSURLRequest *request;
    }
    @property(nonatomic, retain, readonly)UIWebView *rssview;
    @property (nonatomic, assign) NSURLRequest *request;
    @end
    
  3. In DetailViewController.m add delegate method of UIWebview as given below...

    - (BOOL)webView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        return YES;
    }
    
  4. Replace following code in

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
        NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"]; 
        // clean up the link - get rid of spaces, returns, and tabs...
        storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
        storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
        storyLink = [storyLink stringByAddingPercentEscapesUsingEncoding: NSStringEncodingConversionAllowLossy];
        NSLog(@"link: %@", storyLink);
        if(indexPath.row==0)
        {       
            DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
            //[newviewController.rssview loadRequest:request];
            newviewController.request = request;
            [self.navigationController pushViewController:newviewController animated:YES];
        }
        else 
        {   
            DetailViewController *newviewController=[[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]autorelease];
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]];
            //[newviewController.rssview loadRequest:request];
            newviewController.request = request;
            [self.navigationController pushViewController:newviewController animated:YES];
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文