EXC_BAD_ACCESS While switching tabControllers

社会主义新天地 提交于 2019-12-08 10:24:53

问题


I've built an app with three tab controllers. Inside my "Tab1" I'm having dashboard Table.If I click in one of the cell i get a address book table. Now the requirement is when I click on the other Tab2 or Tab3 and come back to Tab1 I should get the first view of the tab1. I used the following code to get this requirement in the Appdelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    if(self.tabbarController.selectedIndex == 0){
        NSArray *viewControllersArray = [self.tabbarController viewControllers];
       [((UINavigationController*)[viewControllersArray objectAtIndex:0]) popToRootViewControllerAnimated:NO];      
    }
}

The problem occurred is when I'm in addressbook table (which is in the tab1), I clicked the tab2 button and come back to the tab1. I m getting EXC_BAD_ACCESS error. I've used NSZombie to find the error and it says

"-[AddressBookViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x12639ca0"

The code works fine with other viewController, except with address book tableView. In the case of address book view controller (which is in the detail page of tab1) I have filtered the contacts containing email address. This is how the code looks like

ABAddressBookRef addressBook = ABAddressBookCreate();
if(addressBook != nil) {

        NSArray *allPeoplearray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger peopleCounter = 0;

        for(peopleCounter = 0;peopleCounter<[allPeoplearray count];peopleCounter++){

            thisPerson = (__bridge ABRecordRef)[allPeoplearray objectAtIndex:peopleCounter];

            NSString *firstName1 = (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty);
            firstName1=[[[firstName1 substringToIndex:1] uppercaseString] stringByAppendingString:[firstName1 substringFromIndex:1]];
            NSString *secondName1 = (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);
            secondName1=[[[secondName1 substringToIndex:1] uppercaseString] stringByAppendingString:[secondName1 substringFromIndex:1]];
            if(secondName1 == NULL){
                secondName1 = @"";
            }
            if(firstName1 == NULL){
                firstName1 = @"";
            }
            long counter =  [self personRecord:thisPerson];

            if(counter == 0){
                NSLog(@"hello");
            }else {
                NSString *Name = [NSString stringWithFormat:@"%@ %@",firstName1,secondName1];

                // [allListArray addObject:[typeName1 NameOfPerson:Name recordNumber:thisPerson]]; 
                NSMutableDictionary *newDict1 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:firstName1,@"firstName",secondName1,@"secondName",thisPerson,@"id",Name,@"fullName", nil];

                [newArray addObject:newDict1];
            }
        }
        // if([[name objectForKey:@"secondName"]isEqualToString:@""] || [[[name objectForKey:@"secondName"]substringToIndex:1] rangeOfCharacterFromSet:notDigits].location == NSNotFound){

        NSSortDescriptor *sortDescriptor;
        sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                     ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        data = [newArray sortedArrayUsingDescriptors:sortDescriptors] ;

        found1 = NO;

        for(NSDictionary *name in data){
            BOOL numberYes = [self NumberExits:[name objectForKey:@"fullName"]];

            if(numberYes){

                if(!found1){
                    [self.sections setValue:[[NSMutableArray alloc] init] forKey:@"#"];
                    found1 = YES;
                }

                NSLog(@" %@",[name objectForKey:@"fullName"]);

            }else {
                 NSLog(@" %@",[name objectForKey:@"fullName"]);
                NSString *chars;
                if([[name objectForKey:@"secondName"]isEqualToString:@""]){
                    chars = [[name objectForKey:@"firstName"]substringToIndex:1];
                }else {
                    chars = [[name objectForKey:@"secondName"]substringToIndex:1];
                }
                found = NO;

                for( NSString *str in [self.sections allKeys]){

                    if([str isEqualToString:chars]){

                        found = YES;
                    }


                }
                if(!found){

                    [self.sections setValue:[[NSMutableArray alloc] init] forKey:chars];

                }
            }
        }

        NSLog(@"%@",[self.sections allKeys]);
        for(NSDictionary *name in data){
             BOOL numberYes = [self NumberExits:[name objectForKey:@"fullName"]];
            if(numberYes){
            [[self.sections objectForKey:@"#"] addObject:name];
            }
            else {
                if([[name objectForKey:@"secondName"]isEqualToString:@""]){
                    [[self.sections objectForKey:[[name objectForKey:@"firstName"] substringToIndex:1]] addObject:name];

                }else {
                    [[self.sections objectForKey:[[name objectForKey:@"secondName"] substringToIndex:1]] addObject:name];
                }

                //[[self.sections objectForKey:[[name objectForKey:@"secondName"] substringToIndex:1]] addObject:name];
            }

            NSLog(@"name = %@",[name objectForKey:@"fullName"]);

        }
        NSLog(@" keys = %@",self.sections );
    }

    NSLog(@"section = %@",self.sections);
}

- (long)personRecord:(ABRecordRef)paramPerson {

    if(paramPerson == nil){

        NSLog(@"The given Person is Null");
    }

    ABMutableMultiValueRef emails = ABRecordCopyValue(paramPerson, kABPersonEmailProperty);
    if(emails == nil){

        return 0;
    }

    NSLog(@"%ld",ABMultiValueGetCount(emails));
    // return (ABMultiValueGetCount(emails));

    return (ABMultiValueGetCount(emails));
}

来源:https://stackoverflow.com/questions/13396970/exc-bad-access-while-switching-tabcontrollers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!