prepareForSegue 推送到 UIWebView
prepareForSegue push to 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];
}
还有其他方法可以实现我想要的吗?谢谢
只需像这样修改您的代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.articleURL];
[self.webView loadRequest:urlRequest];
}
articleURL
已经是 NSURL
类型,所以不需要调用 NSURL *url = [NSURL URLWithString:self.articleURL];
.
更新:
您似乎将 NSString
放入 Article
的 url
属性 中。
在 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;
}
我有一个带有各种单元格的 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];
}
还有其他方法可以实现我想要的吗?谢谢
只需像这样修改您的代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.articleURL];
[self.webView loadRequest:urlRequest];
}
articleURL
已经是 NSURL
类型,所以不需要调用 NSURL *url = [NSURL URLWithString:self.articleURL];
.
更新:
您似乎将 NSString
放入 Article
的 url
属性 中。
在 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;
}