Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在两个控制器之间传递数据,其中一个控制器是在uitableviw中获取的_Ios_Objective C_Uitableview_Uiviewcontroller_Abaddressbook - Fatal编程技术网

Ios 如何在两个控制器之间传递数据,其中一个控制器是在uitableviw中获取的

Ios 如何在两个控制器之间传递数据,其中一个控制器是在uitableviw中获取的,ios,objective-c,uitableview,uiviewcontroller,abaddressbook,Ios,Objective C,Uitableview,Uiviewcontroller,Abaddressbook,我在UITableView上创建了一个ABAddressBook。现在,我需要将名字、姓氏等传递给下一个viewcontroller 我必须使用nsobject类传递数据。。。在这个项目中,我创建了Person类,它具有name、lastname、number、email等字符串属性 通讯簿的代码为: ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook); allPeople = (__bridge NSMutableA

我在
UITableView
上创建了一个
ABAddressBook
。现在,我需要将
名字
姓氏
等传递给下一个viewcontroller

我必须使用nsobject类传递数据。。。在这个项目中,我创建了Person类,它具有name、lastname、number、email等字符串属性

通讯簿的代码为:

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
allPeople = (__bridge NSMutableArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonFirstNameProperty));
NSInteger numberOfPeople = [allPeople count];
for (NSUInteger i = 0; i < numberOfPeople; i++) {
    personContact = [[Person alloc]init];

    person = (__bridge ABRecordRef)allPeople[i];

    NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    if (firstName == nil ) {
        firstName = @"";
    }
    if (lastName == nil)
    {
        lastName = @"";
    }

    NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];

    personContact.firstName = firstName;
    personContact.lastName = lastName;
    personContact.fullName = fullName;

//For adding multiple contacts:



ABMultiValueRef phoneNumbers = ABRecordCopyValue((person), kABPersonPhoneProperty);

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));

        if ([phoneNumber isEqualToString:@""])
        {
            phoneNumber = @"Not Available";
        }

        NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"#();$&-+"];
        phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
        phoneNumber= [phoneNumber stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"" withString:@""];

        personContact.phoneNumber = phoneNumber;

//Emails

        ABMutableMultiValueRef eMail  = ABRecordCopyValue(person, kABPersonEmailProperty);
        if(ABMultiValueGetCount(eMail) > 0)
        {
            email =CFBridgingRelease(ABMultiValueCopyValueAtIndex(eMail, 0));
        }
        else
        {
            email =  @"";
        }
        personContact.email = email;
    }

//Photos

    CFDataRef imgData = ABPersonCopyImageData(person);
    NSData *imageData = (__bridge NSData *)(imgData);

    phoneImage = [UIImage imageWithData:imageData];
    if (phoneImage == nil) {
        phoneImage = [UIImage imageNamed:@"userEdit"];
    }
    CGSize destinationSize = CGSizeMake(70, 70);
    UIGraphicsBeginImageContext(destinationSize);
    [phoneImage drawInRect:CGRectMake(0, 0, destinationSize.width, destinationSize.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if (imgData != NULL)
    {
        CFRelease(imgData);
    }
    personContact.photos = (NSString *)newImage;

    [contactsData addObject:personContact];
    [contactstableView reloadData];

}
}


And the code for cellforrowatIndex path is as follows:- 
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"CellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell==nil)
{

   cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   cell.backgroundColor=[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0];
 }


if (tableView == self.searchDisplayController.searchResultsTableView) {
    personContact = [searchResults objectAtIndex:indexPath.row];

}
else {
    personContact = [contactsData objectAtIndex:indexPath.row];

}
if (contactsData.count > 0)
{
    cell.textLabel.text = personContact.fullName;

    cell.imageView.image = personContact.photos;
    cell.imageView.layer.cornerRadius = 35;
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

  }
ABRecordRef source=ABAddressBookCopyDefaultSource(addressBook);
allPeople=(uu桥NSMutableArray*)(ABAddressBookCopyArrayFallPeopleInSourceWithSortOrdering(addressBook,source,KabbersonfirstNameProperty));
NSInteger numberOfPeople=[allPeople count];
for(整数i=0;i0)
{
email=CfBrigingRelease(ABMultiValueCopyValueAtIndex(email,0));
}
其他的
{
电邮=@;
}
personContact.email=电子邮件;
}
//照片
CFDataRef imgData=ABPersonCopyImageData(个人);
NSData*图像数据=(uu桥NSData*)(imgData);
phoneImage=[UIImage imageWithData:imageData];
如果(phoneImage==nil){
phoneImage=[UIImage ImageName:@“userEdit”];
}
cgsizedestinationsize=CGSizeMake(70,70);
UIGraphicsBeginImageContext(destinationSize);
[phoneImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
如果(imgData!=NULL)
{
CFRelease(imgData);
}
personContact.photos=(NSString*)新图像;
[联系人数据添加对象:personContact];
[联系人StableView重新加载数据];
}
}
CellForRowatinex路径的代码如下:-
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=@“CellID”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];
cell.backgroundColor=[UIColor COLOR WITHRED:238.0/255.0绿色:238.0/255.0蓝色:239.0/255.0阿尔法:1.0];
}
if(tableView==self.searchDisplayController.searchResultsTableView){
personContact=[searchResults objectAtIndex:indexPath.row];
}
否则{

personContact=[ContactsDataObjectAtIndex:indexath.row]; } 如果(contactsData.count>0) { cell.textlab.text=personContact.fullName; cell.imageView.image=personContact.photos; cell.imageView.layer.cornerRadius=35; cell.imageView.layer.masksToBounds=是; cell.imageView.contentMode=UIViewContentModeScaleAspectFit; }

didselect
行中,我想将所有联系人详细信息传递给下一个控制器。请帮助?

如果必须将其从地址簿传递到ViewController,请使用委派方法

如果必须将其传递给地址簿,则可以传递个人或关联属性

objc_setAssociatedObject(actionSheet, @"selectedUserJidStr", selectedUserJidStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

我猜您将在单元格选择上显示视图,您可以使用以下委托方法执行此操作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"segueName" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segueName"])
    {
        NSIndexPath *indexPath = sender;
        ViewControllerName *objectOfViewControllerName = segue.destinationViewController;
        objectOfViewControllerName.personContact = [contactsData objectAtIndex:indexPath.row];
    }
}

我在cellforrowAtIndexPath做这个

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {
    AddGuestVC *add = [[AddGuestVC alloc]initWithNibName:@"AddGuestVC" bundle:nil];

    add.personContact = [contactsData objectAtIndex:indexpath.row];

    add.delegate = self;
   add.personContact = personContact;
    [self.navigationController pushViewController:add animated:YES];
}
}
对于填充文本字段,我使用:

   firstname.text = self.personContact.firstName; 

我已经在uitableview中获取了如上所示的数据,现在我需要传递cell@ankitpersonContact=[contactsData objectAtIndex:indexPath.row]的数据;我需要与此示例相同的cellForRowAtIndexPAth实现appcoda.com/search-bar-tutorial-ios7@ankitIn下一个视图控制器创建属性,并从TableViewController在选定的索引处使用对象填充属性我已经这样做了,但下一个字段中的文本字段没有填充可能与我需要的重复与此示例相同的CellForRowatineXpath实现