我用三个标签控制器构建了一个应用程序。在我的“Tab1”里面我有仪表板表。如果我点击其中一个单元格,我会得到一个地址簿表。现在要求是当我点击另一个Tab2或Tab3并返回到Tab1时,我应该得到tab1的第一个视图。我使用以下代码在Appdelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if(self.tabbarController.selectedIndex == 0){
NSArray *viewControllersArray = [self.tabbarController viewControllers];
[((UINavigationController*)[viewControllersArray objectAtIndex:0]) popToRootViewControllerAnimated:NO];
}
}
出现的问题是当我在addressbook table
(在tab1中)时,我点击了tab2按钮并返回到tab1。我收到 EXC_BAD_ACCESS 错误。我使用NSZombie
来查找错误,并说
"-[AddressBookViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x12639ca0"
除了地址簿tableView之外,代码与其他viewController一起正常工作。对于地址簿视图控制器(位于tab1的详细页面中),我已经过滤了包含电子邮件地址的联系人。这就是代码的样子
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));
}