【问题标题】:prepareForSegue push to UIWebViewprepareForSegue 推送到 UIWebView
【发布时间】:2015-01-05 19:18:02
【问题描述】:

我有一个包含各种单元格的 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;
    }
}

视图控制器

@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];
}

还有其他方法可以实现我想要的吗?谢谢。

【问题讨论】:

    标签: ios objective-c iphone uiwebview


    【解决方案1】:

    只需像这样修改您的代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.articleURL];
        [self.webView loadRequest:urlRequest];
    }
    

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

    更新: 您似乎将NSString 放入Articleurl 属性中。

    Article.m 中尝试以下操作:

    - (instancetype)initWithAttributes:(NSDictionary *)attributes {
        self = [super init];
        if (!self) {
            return nil;
        }
        NSString *urlString = attributes[@"url"]; // first get the URL as a string from attributes
        self.url = [NSURL URLWithString:urlString]; // then assign it to property of type NSURL
    
        return self;
    }
    

    【讨论】:

    • 我试过了,但我得到了这个错误:*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[__NSCFString absoluteURL]: unrecognized selector sent to instance
    • 啊,好吧,这意味着您在CollectionViewController 中存储的article.url 实际上是NSString。你能贴出Article.m 的代码并告诉在哪里你分配article.url
    • 请更新您的问题,而不是将其发布在 cmets 中 :)
    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多