1

我想将数据从 TableView 传递给 anotherViewController。但我不知道如何传递数据。这是我的代码。

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     PeripheralManager    * objSelectedDevice=[device objectAtIndex:indexPath.row];

   // [self prepareForSegue:@"TableDetails" sender:objSelectedDevice];
   // detailViewObject.objCurrentDevice=objSelectedDevice;
}

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


    if ([segue.identifier isEqualToString:@"TableDetails"]) {

        MeBleDetailViewController *detailViewObject=segue.destinationViewController;


        detailViewObject.dataArray=device;

        detailViewObject.title=@"Supported Services";

        detailViewObject.objCurrentDevice=pobjSelectedDevice;
    }
}
4

2 回答 2

3

您需要创建一个实例变量,在调用 之前对其进行设置,然后在 中performSegueWithIdentifier:获取其值prepareForSegue:,如下所示:

在标题中:

@interface MyViewController {
    //... your ivars ...
    PeripheralManager *selectedManager;
}
//... more stuff ...
@end

在实施中:

-(void)tableView:(UITableView *)tableView
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // set your ivar
    selectedManager = [device objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"TableDetails" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"TableDetails"]) {

        MeBleDetailViewController *detailViewObject=segue.destinationViewController;

        detailViewObject.dataArray=device;

        detailViewObject.title=@"Supported Services";

        // read your ivar
        detailViewObject.objCurrentDevice=selectedManager;
    }
}
于 2012-10-27T12:00:00.653 回答
2

你不调用prepareForSegue:你的代码;打电话给performSegueWithIdentifier:你,你会prepareForSegue:打电话给你的。

于 2012-10-27T11:50:13.927 回答