prepareForSegue推送到UIWebView

时间:2015-01-05 19:18:02

标签: ios objective-c iphone uiwebview

我有一个包含各种单元格的CollectionViewController。每当我点击一个单元格时,我想要推送到一个带有UIWebView的视图。每个单元格都有不同的URL。

我已设法让代码正常运行但我收到此错误:不兼容的指针类型发送' NSURL *'参数类型' NSString *'

Article.h

@interface Article : NSObject

@property (nonatomic, strong) NSURL *url;

- (instancetype)initWithAttributes:(NSDictionary *)attributes;

+ (void)articlesWithBlock:(void (^)(NSArray *articles, NSError *error))block;

@end

Article.m

@implementation Article

- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.url = attributes[@"url"];

    return self;
}

+ (void)articlesWithBlock:(void (^)(NSArray *articles, NSError *error))block {
    [[DeadstockAPIManager sharedManager] GET:@"articles" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
        NSMutableArray *mutableArticles = [NSMutableArray array];
        for (NSDictionary *attributes in JSON[@"articles"]) {
            Article *article = [[Article alloc] initWithAttributes:attributes];
            [mutableArticles addObject:article];
        }
        if (block) {
            block([NSArray arrayWithArray:mutableArticles], nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
}

@end

CollectionViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showArticle"]) {
        NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];
        Article *article = [self releaseForIndexPath:selectedIndexPath];
        ArticleViewController *articleViewController = (ArticleViewController *)segue.destinationViewController;
        articleViewController.articleURL = article.url;
    }
}

的ViewController

@property (nonatomic, strong) NSURL *articleURL;

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

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:self.articleURL]; **// This is where I get the error**
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];
}

我可以通过其他任何方式实现我的目标吗?感谢。

1 个答案:

答案 0 :(得分:0)

只需修改您的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.articleURL];
    [self.webView loadRequest:urlRequest];
}

articleURL已经是NSURL类型,因此无需致电NSURL *url = [NSURL URLWithString:self.articleURL];

<强>更新 您似乎将NSString放入url的{​​{1}}属性中。

Article中尝试以下内容:

Article.m