gpt4 book ai didi

ios - 在 UIWebView 中显示来自不同 View Controller 的 URL

转载 作者:行者123 更新时间:2023-11-29 10:33:07 25 4
gpt4 key购买 nike

   RecipesTableViewController.m

#import "RecipesTableViewController.h"
#import "RecipeTableViewCell.h"
#import "IngredientsViewController.h"
#import "Recipe.h"
#import "RecipeDetailViewController.h"

@interface RecipesTableViewController () {

NSMutableArray *recipesArray;

}

@end

@implementation RecipesTableViewController

- (void)viewDidLoad {
[super viewDidLoad];

//api from recipepuppy
NSString *recipeUrlString = [NSString stringWithFormat:@"http://www.recipepuppy.com/api/?i=%@",self.searchRecipe];

//adding percentage on the textfield when the user is searching
NSString *formattedString = [recipeUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//download data
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: formattedString]];


//put data into a dictionary
NSDictionary *recipeDictinary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

//then put the dictionary into an array
recipesArray = [[NSMutableArray alloc]init];
for (NSDictionary *recipeDict in [recipeDictinary objectForKey:@"results"]) {


Recipe *recipe = [[Recipe alloc]initWithTitle:[recipeDict objectForKey:@"title"] andRecipeIngredients:[recipeDict objectForKey:@"ingredients"] andImageURL:[NSURL URLWithString:[recipeDict objectForKey:@"thumbnail"]] andRecipeWebUrl:[recipeDict objectForKey:@"href"]];


[recipesArray addObject:recipe];
NSLog(@"%@", recipeDict);
}
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.

return [recipesArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RecipeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recipeCell" forIndexPath:indexPath];

[cell drawTheCell:[recipesArray objectAtIndex:indexPath.row]];

return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"recipeDetail"]) {
//NSIndexPath *indexPath =[self.tableView indexPathForSelectedRow];

RecipeDetailViewController *recipeDetail = segue.destinationViewController;

recipeDetail.title = @"Recipe";


}

}

@end

短篇小说: 我正在为我的类(class)制作一份配料食谱。 我有一个 UITableViewControllre 解析来自 api 的内容,并且我在数组中有 api 的对象。在那个数组中,我有“结果”,在这些结果中,我有 url、标题、成分和食谱图像。我想将 WebView 的 url 发送到另一个 View Controller ,但我做不到。每当我选择食谱时,应用程序都会崩溃以查看 WebView 。我被困在这个问题上三天了,我很沮丧,我知道问题出在我链接到 webview 上,因为数组打印了 url 但没有显示在 webview 上。

这是我的 table view controller,我的 api 和 prepare for segue 到 webview 所在的 view controller。

    RecipeTableViewCell.m

#import <UIKit/UIKit.h>
#import "Recipe.h"

@interface RecipeTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *recipeUrl;
@property (strong, nonatomic) IBOutlet UILabel *recipeTitle;
@property (strong, nonatomic) IBOutlet UILabel *recipeIngredients;
@property (strong, nonatomic) IBOutlet UIImageView *recipeImage;

-(void)drawTheCell:(Recipe *)recipeObject;

@end

RecipeTableViewCell.m

-(void)drawTheCell:(Recipe *)recipeObject {

self.recipeTitle.text = recipeObject.title;

self.recipeIngredients.text = recipeObject.ingredients;

self.recipeUrl.text = recipeObject.recipeWebUrl;

NSData *imageData = [NSData dataWithContentsOfURL:recipeObject.imageURL];
self.recipeImage.image = [UIImage imageWithData:imageData];

#import "RecipeDetailViewController.h"
#import "RecipeTableViewCell.h"



@interface RecipeDetailViewController ()
@property (strong, nonatomic) IBOutlet UIWebView *recipeWebView;

@end

RecipeDetailViewController.m

@implementation RecipeDetailViewController

- (void)viewDidLoad {
[super viewDidLoad];

Recipe *recipe = [[Recipe alloc] init];

NSURL *url = [NSURL URLWithString: recipe.recipeWebUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

[self.recipeWebView loadRequest:request];


}

RecipeDetailViewController.h

#import <UIKit/UIKit.h>

@interface RecipeDetailViewController : UIViewController
@property (nonatomic, strong ) NSString *recipeWebUrlString;

这是我的单元格,在这里显示标题、成分和图像,并且工作正常。

最佳答案

Skyler 的回答朝着正确的方向前进,但缺少一些关键部分......

是的,您需要像他建议的那样在 prepareForSegue: 中传递 web url 字符串,即

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
if([segue.identifier isEqualToString:@"recipeDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeTableViewCell *cell = (RecipeTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];

RecipeDetailViewController *recipeDetail = segue.destinationViewController;

recipeDetail.title = @"Recipe";
recipeDetail.recipeWebUrlString = cell.recipeUrl.text;
}
}

但问题是您没有使用 recipeWebUrlString 来执行您的 url 请求。

相反,您在 .m 中创建了一个空的 Recipe 对象,因此使用一个空的 url 来执行网络请求,即

Recipe *recipe = [[Recipe alloc] init];
NSURL *url = [NSURL URLWithString: recipe.recipeWebUrl];

将这两行 (^) 替换为以下内容:

NSURL *url = [NSURL URLWithString:self.recipeWebUrlString];

为了使用您刚刚从 RecipesTableViewController 传入的 url。

关于ios - 在 UIWebView 中显示来自不同 View Controller 的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28532844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com