2012-10-30 52 views
1

我在NSObject類中有一個MutableArray我想將NSMutableArray傳遞給detailViewController然後我想在tableView(detailViewController)中顯示NSMutableArray。在這個項目中我有一個兩個視圖控制器,一個是firstViewcontroller和detailviewcontroller.the問題是數據不能從NSObject傳遞。給我一個想法,如果你想要更多信息請詢問。錯誤從NSObject傳遞NSMutableArray到ViewController

peripheralmanager.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"); 

} 

detailviewcontroller.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 *[email protected]"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) 

我想這樣image..Please輸出給一個idea..Please問我瞭解更多信息。

enter image description here

+0

我想'[self.mutableArray ADDOBJECT:外圍]'可能會增加「無」 – BabyPanda

+0

多少次你要問同樣的問題? – Abizern

+0

@Abizern仍然錯誤我嘗試了很多方法。不要生氣很酷。 – prem

回答

2

您可以使用指定初始化器來解決這個問題

在你FirstViewController

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

在DetailedViewController你可以調用這個方法..

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

你可以看到你的陣列將被裝入數據

+1

兄弟謝謝... :) – prem

1

我不認爲你閱讀異常消息:

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

此異常無關,與周圍傳遞數組,它涉及到你是如何填充數組。可可收藏班不能容納nil指針(如果你想要類似的東西,請參閱NSNull),並且你正在試圖將一個nil指針放入數組中,這對於(字面上)來說是個例外。

添加一些後衛代碼:

obj.dataArray=self.mutableArray; 
if (peripheral != nil) 
    [self.mutableArray addObject:peripheral]; 
+0

沒有朋友同樣的錯誤顯示。 – prem

+0

@prem好了,您需要發佈符號堆棧跟蹤(地址列表不夠)。 – trojanfoe

+0

@Abizern仍然錯誤我嘗試了很多方法。 不要生氣酷。 – prem

相關問題