1

我在 NSObject 类中有一个 MutableArray 我想将 NSMutableArray 传递给 detailViewController 然后我想在 tableView(detailViewController) 中显示 NSMutableArray。在这个项目中我有两个视图控制器,一个是 firstViewcontroller 和 detailviewcontroller。问题是数据无法通过来自 NSObject。如果您需要更多信息,请给我一个想法请询问。

外围管理器.m

- (void)centralManager:(CBCentralManager *)central 
didDiscoverPeripheral:(CBPeripheral *)peripheral 
 advertisementData:(NSDictionary *)advertisementData 
              RSSI:(NSNumber *)RSSI
{

  NSLog(@"didDiscoverPeriphera.peripheral: %@ rssi: %@, UUID:%@ advertisementData:%@", peripheral,RSSI,peripheral.UUID, [advertisementData description]);

  targetPeripheral = peripheral;
  peripheral.delegate = self;
  //  [[centralManager retrievePeripherals:NSArray arrayWithObject:(id)peripheral.UUID] ];
  //  rssilabel.text=[RSSI stringValue];
  if (!peripheral.isConnected)
  {
  [centralManager connectPeripheral:peripheral options:nil];
  }
  // MeBleAppDelegate *appdelegate=(MeBleAppDelegate *)[[UIApplication sharedApplication]delegate];
  MeBleDetailViewController *obj=[[MeBleDetailViewController alloc]init];
  // MeBleDetailViewController *obj=[[appdelegate sharedappdelegate].navigationController objectATindex:MeBleDetailViewController];
  obj.dataArray=self.mutableArray;
  [self.mutableArray addObject:peripheral];
  printf("New UUId,addin\r\n");

}

详细视图控制器.m

- (void)viewDidLoad
{
  //NSMutableArray *dataArray=[[NSMutableArray alloc]init];
  [super viewDidLoad];
  NSLog(@"%@",self.dataArray);
  NSLog(@"%@",objCurrentDevice.mutableArray);
  //  PeripheralManager *peripheralmanager=[[PeripheralManager alloc]init];
  self.label.text=[NSString stringWithFormat:@" %@ ",dataArray];
  // Do any additional setup after loading the view.
  // [self.dataArray addObject:objCurrentDevice.mutableArray];

  appDelegate.arrDevices=dataArray;
  appDelegate = [[UIApplication sharedApplication] delegate]; 
  // write below line in any function in this file
  // NSLog(@"%d",[appDelegate.arrDevices count]);  
}

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

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

    cell.textLabel.text=[dataArray objectAtIndex:indexPath.row];
     //  PeripheralManager *objtemp=[[dataArray objectAtIndex:indexPath.row]deviceName];
     NSLog(@"%@",objCurrentDevice.deviceName);
    return cell;
}

错误信息

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***    -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

*** First throw call stack:

(0x356c888f 0x37a6f259 0x3561d1d7 0x164db 0x33135c8b 0x331421e9 0x33142059 0x33141f3f 0x331417c1 0x33141503 0x33135aff 0x331357d5 0x331ae903 0x33228627 0x351e2933 0x3569ca33 0x3569c699 0x3569b26f 0x3561e4a5 0x3561e36d 0x372ba439 0x3312acd5 0x1500f 0x14fb4)

terminate called throwing an exception(lldb) 

我想要这样的图像输出..请给出一个想法..请向我询问更多信息。

在此处输入图像描述

4

2 回答 2

2

您可以使用指定初始化程序来解决此问题

在你的FirstViewController

-(id)initwithData (NSArray*)array
{
  //store the data in array
}

在DetailedViewController 你可以调用这个方法..

MeBleDetailViewController *obj=[[MeBleDetailViewController alloc]initwithData:yournsarrayname];

您可以看到您的数组将加载数据

于 2012-10-30T09:08:36.787 回答
1

我认为您没有阅读异常消息:

-[__NSArrayM insertObject:atIndex:]: object cannot be nil'

此异常与传递数组无关,它与您填充数组的方式有关。Cocoa 集合类不能保存nil指针(看看NSNull你是否想要这样的东西)并且你正试图将一个nil指针放入数组中,它对(字面意思)例外。

添加一些保护代码:

obj.dataArray=self.mutableArray;
if (peripheral != nil)
    [self.mutableArray addObject:peripheral];
于 2012-10-30T09:07:44.913 回答