2

Im trying to make a small project. Could someone help me understand why the segue does not work?

#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarDetailViewController.h"

@interface CarTableViewController ()




@end

@implementation CarTableViewController

@synthesize carMakes = _carMakes;
@synthesize carModels = _carModels;
@synthesize carImages = _carImages;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    self.carMakes = [[NSArray alloc]
                     initWithObjects:@"Chevy",
                     @"BMW",
                     @"Toyota",
                     @"Volvo",
                     @"Smart", nil];

    self.carModels = [[NSArray alloc]
                      initWithObjects:@"Volt",
                      @"Mini",
                      @"Venza",
                      @"S60",
                      @"Fortwo", nil];

    self.carImages = [[NSArray alloc]
                      initWithObjects:@"chevy_volt.jpg",
                      @"mini_clubman.jpg",
                      @"toyota_venza.jpg",
                      @"volvo_s60.jpg",
                      @"smart_fortwo.jpg", nil];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (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 _carModels.count;
}



#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"carTableCell";

    CarTableViewCell *cell = [tableView
                              dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CarTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.makeLabel.text = [self.carMakes
                           objectAtIndex: [indexPath row]];

    cell.modelLabel.text = [self.carModels
                            objectAtIndex:[indexPath row]];

    UIImage *carPhoto = [UIImage imageNamed:
                         [self.carImages objectAtIndex: [indexPath row]]];

    cell.carImage.image = carPhoto;

    return cell;
}

The code work´s fine, and loads a tableView, but I need to go to CarDetailViewControler and I´m using the following code, but it does not work.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"ShowCarDetails"])
    {
        CarDetailViewController *detailViewController =
        [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView
                                    indexPathForSelectedRow];

        detailViewController.carDetailModel = [[NSArray alloc]
                                               initWithObjects: [self.carMakes
                                                                 objectAtIndex:[myIndexPath row]],
                                               [self.carModels objectAtIndex:[myIndexPath row]],
                                               [self.carImages objectAtIndex:[myIndexPath row]],
                                               nil];
    }
}
4

1 回答 1

4

在情节提要中,您是否使用了单元原型?如果是这样,您是否从原型控制拖动到目标视图控制器并创建命名的segue?

您使用的方法要求在情节提要中完全定义 segue,这意味着 segue 的触发器与单元原型相关联。

您可以使用以下方法手动触发 segue:

[self performSegueWithIdentifier:@"ShowCarDetails" sender:self];

在你的:

tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath作为备选。

于 2013-02-22T01:52:40.680 回答