IOS Segue-检测表格单元格并打开viewController

时间:2014-01-02 11:02:45

标签: ios uitableview uistoryboardsegue

我是ios编程的新手。我有一个静态tableViewController,其中我有3个单元格。我有另一个视图控制器,其中有标签和说明。我想要做的是检测用户点击的单元格,然后根据那个更改我的第二个视图控制器中的标签,但问题是每当我点击单元格程序崩溃并添加断点

这是我的代码

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
      NSString *name;
      NSString *description;

    if([[segue identifier] isEqualToString:@"PushAppDetailsFromCell1"] )
    {
        name = @"Label 1 ";
        description = @"Long description of Label 1...";
    }

    else if([[segue identifier] isEqualToString:@"PushAppDetailsFromCell2"] )
    {
        name = @"Label 2";
        description = @"Long description of Label 2...";

    }
    else if([[segue identifier] isEqualToString:@"PushAppDetailsFromCell3"] )
    {
        name = @"Label 3";
        description = @"Long description of Label 3...";
    }
        else {
            return;

}
        AppDetailsViewController *apDetailsViewController = 
        segue.destinationViewController;  //here i am getting the breakpoint
        apDetailsViewController.appDetails =
        [[AppDetails alloc] initWithName:name description:description];

}

AppDetails.m

-(id)initWithName:(NSString *)name description:(NSString *)descr{

    self = [super init];

    if(self){
        self.name = name;
        self.description = descr;

        }
    return self;
}

AppDetailsViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.nameLabel.text = self.appDetails.name;
    self.descriptionLabel.text = self.appDetails.description;

}

3 个答案:

答案 0 :(得分:2)

找出问题的步骤

1)在prepareForSegue中加入一个断点

2)尝试看到它显示正确的segue id(可能会出现拼写错误)。

3)看看它在哪里崩溃

 - In prepareForSegue?
 - Is this calling initname()?
 - has it started it viewDidLoad().

如果你这样做,你会发现问题所在。如果你不能告诉我。

答案 1 :(得分:1)

将segue从UIViewController绑定到UIViewController而不是单元格到UIViewController。实现以下导航代码。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"%ld",aIntSelected);
    NSLog(@"%@",segue.destinationViewController);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   aIntSelected=indexPath.row;
   NSLog(@"didSelectRowAtIndexPath called");
   [self performSegueWithIdentifier:@"pushSecond" sender:self];

}

答案 2 :(得分:0)

NSArray *names = @[@"Label 1", @"Label2", @"Label 3"];
NSArray *descs = @[@"Description 1", @"Description", @"Description 3"];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PushAppDetailsSegue"]) {
         NSIndexPath *indexPath = [self.yourTableView indexPathForSelectedRow];
        AppDetailsViewController *controller = segue.destinationViewController;
        controller.appDetails = [[AppDetails alloc] initWithName:names[indexPath.row] description:descs[indexPath.row]];
    }
}

将segue从ViewController拖到SecondViewController并将其命名为“PushAppDetailsS​​egue”。