没有故事板的表视图控制器的详细视图控制器? [英] Detail View Controller from Table View Controller WITHOUT Storyboard?

查看:37
本文介绍了没有故事板的表视图控制器的详细视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有一个 tableview,用于显示收据及其图像、创建日期等.
我这样做是为了当用户点击一个单元格时,它会转到 DetailViewController.使用此代码:

I have a tableview in a controller that shows receipts and their image, date created, etc.
I made it so when the user taps a cell, it goes to DetailViewController. Using this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    ReceiptDetailViewController *controller = [[ReceiptDetailViewController alloc] initWithNibName:@"ReceiptDetailViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

我想通过 UIImageViewtextView 等将图像、日期、类别等传递给我的新视图.
这是第一个控制器中我的表的代码:

I'd like to pass the image, date, category, etc, to my new view in a UIImageView, textView, etc.
This is the code for my Table in the first controller:

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    Receipt *receipt = [self.receiptsArray objectAtIndex:indexPath.row];

    ReceiptCategory *category = receipt.relationshipToCategory;
    ReceiptImage *image = receipt.relationshipToImage;

    cell.textLabel.text = category.receiptCategory;
    cell.detailTextLabel.text = receipt.date.description;

    if(self.imageCache.count <= indexPath.row) {
        [self.imageCache addObject:[UIImage imageWithData:image.data]];
    }

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    imageView.image = [self.imageCache objectAtIndex:indexPath.row];

    cell.imageView.image = imageView.image;

    return cell;
}

我如何传递这些数据?我已经搜索了两天,所有解决方案都包含一个我没有使用的故事板.

How do I pass this data? I have searched around for two days now and all solutions include a storyboard, which I am not using.

推荐答案

您应该在 didSelectRowAtIndexPath: 中获得对您的收据的引用,并将其传递给 ReceiptDetailViewController.我假设你有属性来接受 ReceiptDetailViewController 中的数据:

You should get reference to your receipt in didSelectRowAtIndexPath: and pass it to ReceiptDetailViewController. I assume you have property to accept the data in ReceiptDetailViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get reference to receipt
    Receipt *receipt = [self.receiptsArray objectAtIndex:indexPath.row];

    ReceiptDetailViewController *controller = [[ReceiptDetailViewController alloc] initWithNibName:@"ReceiptDetailViewController" bundle:nil];

    // Pass data to controller
    controller.receipt = receipt;
    [self.navigationController pushViewController:controller animated:YES];
}

在此示例中,您应该在 ReceiptDetailViewController.h 文件中创建属性:

It this example you should create property in ReceiptDetailViewController.h file:

@property(nonatomic, strong) Receipt *receipt;

现在您可以在您的 ReceiptDetailViewController 对象中使用它.

Now you can use it in your ReceiptDetailViewController object.

这篇关于没有故事板的表视图控制器的详细视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆