在 UIWebView 中显示来自不同视图控制器的 URL

Displaying an URL from a different view controller in a UIWebView

   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 的 objects。在那个数组中,我有“结果”,在这些结果中,我有 urls、标题、成分和食谱图像。我想将 url 发送到 WebView 到另一个视图控制器,但我做不到。每当我 select 食谱时,应用程序崩溃以查看 webview。我被困在这个问题上三天了,我很沮丧,我知道问题出在我链接到 webview 上,因为数组打印了 url 但没有显示在 webview 上。

这是我的 table 视图控制器,我的 api 位于其中,并准备转至 Web 视图所在的视图控制器。

    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;

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

这里有一个冗长的答案,但我会尽量保持简短,希望它有意义。

首先,我没有看到您在哪里调用 performSegueWithIdentifier: 这意味着您可能正在通过情节提要从直接单击单元格到下一个视图进行转场。这对于简单的按钮按下非常有用,但对于选择您需要从中发送信息的单元格来说并不理想。我建议在 didSelectRowAtIndexPath: 中调用 segue。故事板上的 segue 应该直接从一个视图控制器转到另一个视图控制器,而不是直接从 table 单元格。不要忘记再次设置标识符。

像这样在代码中调用 segue。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"recipeDetail" sender:nil]; //you could also pass the cell with if you want
}

其次,在准备 segue 时,您没有设置所需的 URL,而只是设置下一个视图控制器的标题。看起来您已经接近您想要的,因为我可以看到您已经在查看索引路径但将其注释掉了。您应该抓住那里的单元格并将 url 设置为 recipeDetail。您也可以通过发件人传递单元格。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{

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

        RecipeDetailViewController *recipeDetail = segue.destinationViewController;

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


    }
}

第三件事,如果一切都失败了,那就开始到处放 NSLogs 吧。您可以在下一个视图中加载的视图中记录 URL 并查看它是否未设置。接下来你应该看看你在哪里设置它,据我所知,这似乎不在任何地方=)

尽管如此,我不会转述单元格上的文本,而是从您的数组中获取食谱并将其传递到 segue 中。

希望对您有所帮助,或者至少让您指明正确的方向。

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

是的,您需要像他建议的那样在 prepareForSegue: 中传递网络 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。