【问题标题】:How to pass the properties of data to anotherViewController?(StoryBoard)如何将数据的属性传递给另一个视图控制器?(故事板)
【发布时间】:2012-10-28 05:09:52
【问题描述】:

我想在选择 tableviewcell 后传递数据的属性。请指导我。我已经设置了 detailviewcontroller。DataArray 是我在 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;


}
}

【问题讨论】:

  • 视图控制器是一个对象,您可以像在任何其他情况下一样通过调用对象上的方法来传递数据。

标签: iphone objective-c ios uitableview ios5


【解决方案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]];

【讨论】:

  • 不,idx 只是选定元素索引的 NSInteger。在您的 DetailViewController 中定义 @property(readwrite,assign) NSInteger idx,在 viewWillAppear 中您将获得当前索引。但是,我建议不要将整个数组和索引传递给细节控制器,最好只传递活动元素。在这种情况下,你会做这样的事情:NSIndexPath *indexPath = [self.deviceTable indexPathForCell:sender]; detail.currentElem = [device objectAtIndex:[indexPath row]];
  • 你能帮我查一下吗@whiteagle
  • 那里你不需要实现 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,只需在 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 更改为: detailViewObject.objCurrentDevice=[device objectAtIndex:[[self.deviceTable indexPathForCell:sender] row]];
  • 感谢您的宝贵时间..:)..还有一个问题朋友如何将 NSMutableArray 从 NSObject 类传递给 ViewController..请指导我
  • 我不知道你在问什么,你能详细点吗?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-05-02
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多