如何将数据属性传递给另一个ViewController?(StoryBoard)

时间:2012-10-28 05:09:53

标签: iphone objective-c ios uitableview ios5

我想在选择一个tableviewcell之后传递数据的属性。请指导我。我已经设置了detailviewcontroller.DataArray是我的mutablearray在detailviewcontroller.peripheralManager是我的NSbject.Peripheral是我的设备。

- (void)viewDidLoad
{
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
  peripheralManager=[[PeripheralManager alloc]init];
  self.MeBle.enabled=NO;
  device=[[NSMutableArray alloc]init];

}
- (void)peripheralManagerDidConnectPeripheral:(PeripheralManager *)peripheral
{
  NSLog(@"%@",peripheral.deviceName);
  [device addObject:[NSString stringWithFormat:@" %@ ",peripheral.deviceName]];    
   //   [device addObject:peripheral];
  [self.deviceTable reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *simpleTableIdentifier=@"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  if(cell==nil)
{
    cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
   cell.textLabel.text=[device objectAtIndex:indexPath.row];
   return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //DetailViewController *detail=[self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
   // [self.navigationController pushViewController:detail animated:YES];

   //[self performSegueWithIdentifier:@"TableDetails" sender:[device objectAtIndex:indexPath.row]];

}

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


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

    DetailViewController *detail=segue.destinationViewController;
    detail.dataArray=device;


}
}

2 个答案:

答案 0 :(得分:1)

在prepareForSegue中,你应该得到当前的选择索引:

NSIndexPath *indexPath = [self.deviceTable indexPathForCell:sender];

现在,您可以将此索引传递给目标控制器或当前数组中的特定对象,由您自己决定。

detail.idx = [indexPath row];

编辑,您无需实施

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

就在

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

改为:

detailViewObject.objCurrentDevice=[device objectAtIndex:[[self.deviceTable indexPathForCell:sender] row]];

答案 1 :(得分:1)

在界面构建器中,查看seague运行时属性部分,你可以创建多个单元格相同的样式但不同的标识符然后cellForRowAtIndexPath按行返回单元格