IOS XMPP如何使用特定用户名和聊天进入下一个控制器

时间:2015-06-03 07:19:21

标签: ios objective-c xmpp

我正在研究XMPP项目。我已成功完成登录过程和在线热线名单。但现在我不知道如何使用特定用户的字段进入下一个视图控制器并与他聊天。这是我的尝试。 现在我要用UITableview的代表方法写一下

friendsviewcontroller.m file // Fetch online and offline rosterlist

  #pragma mark -Tableview datasource method
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
    return [[[self fetchedResultsController] sections] count];
}
    - (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row){
    case 0:
        if(indexPath.section==0)
            return 60.0; // first row is 123pt high
    default:
        return 60.0; // all other rows are 40pt high
 }
 }
   - (NSString *)tableView:(UITableView *)sender titleForHeaderInSection:(NSInteger)sectionIndex
{
NSArray *sections = [[self fetchedResultsController] sections];
   if (sectionIndex < [sections count])
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
    int section = [sectionInfo.name intValue];
    switch (section)
    {
        case 0  : return @"Available";
        case 1  : return @"Away";
        default : return @"Offline";
    }
}
 return @"";}
   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex{
NSArray *sections = [[self fetchedResultsController] sections];

if (sectionIndex < [sections count])
 {
     id <NSFetchedResultsSectionInfo> sectionInfo = sections[sectionIndex];
     return sectionInfo.numberOfObjects;
 }
return 0;}
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
  static NSString *simpleTableIdentifier = @"SimpleTableItem";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
 if( cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
    }
    XMPPUserCoreDataStorageObject *user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        cell.textLabel.text = user.displayName;
            [self configurePhotoForCell:cell user:user];
   // cell.detailTextLabel.text= [self.tblchathistory objectAtIndex:indexPath.row];
    cell.textLabel.textColor=[UIColor whiteColor];
    return cell;
   }
   #pragma Mark - segue method
  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
     if ([[segue identifier] isEqualToString:@"chathistory"])
    {
   CreateGroupViewController *vc = [segue destinationViewController];
    }
  }
  - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
NSXMLElement *queryElement = [iq elementForName: @"query" xmlns: @"jabber:iq:roster"];
if (queryElement)
{
    NSArray *itemElements = [queryElement elementsForName: @"item"];
    for (int i=0; i<[itemElements count]; i++)
    {
        NSLog(@"Friend: %@",[[itemElements[i] attributeForName:@"jid"]stringValue]);
        }
}
return NO;
}
 - (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController == nil)
{
    NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
                                         inManagedObjectContext:moc];

    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
    NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setFetchBatchSize:10];

    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                   managedObjectContext:moc
                                                                     sectionNameKeyPath:@"sectionNum"
                                                                              cacheName:nil];
    [fetchedResultsController setDelegate:self];


    NSError *error = nil;
    if (![fetchedResultsController performFetch:&error])
    {
        DDLogError(@"Error performing fetch:= %@", error);
        //NSLog(@"error = %@", error);
    }
}

return fetchedResultsController;
 }

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[[self tblvwbuddy] reloadData];
 }

1 个答案:

答案 0 :(得分:1)

您可以按照以下方式获取用户 当您在一个屏幕中显示意味着您拥有用户的jid时,请使用该用户的jid并在下一个控制器中过滤用户。 这是我的代码,通过jid过滤来自XMPPUserCoreDataStorageObject的porticular用户。在我的情况下,friendJid是要过滤的jid

- (XMPPUserCoreDataStorageObject *)fetchTheUser
{
    NSManagedObjectContext *moc = [APP_DELEGATE managedObjectContext_roster];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
                                              inManagedObjectContext:moc];

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"jidStr=%@",[self.friendJid lowercaseString]];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:pred];

    NSError *error = nil;

    [fetchRequest setPredicate:pred];
    NSArray *fetchedObjects = [[APP_DELEGATE managedObjectContext_roster] executeFetchRequest:fetchRequest error:&error];

    XMPPUserCoreDataStorageObject *objTemp = fetchedObjects.count?[fetchedObjects objectAtIndex:0]:nil;

    return objTemp;
}

希望它会对你有所帮助。 如果你发现这个有用的

,请接受答案