Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:37:05+00:00 2026-06-06T14:37:05+00:00

I have two classes, class A and class B. Class A has a mission

  • 0

I have two classes, class A and class B. Class A has a mission not related to chat BUT it needs to react to the arrive of a chat message. When a message arrives, it is stored in a NSMutableDictionary, being that dictionary stored in a NSMutableArray. If the message received is the first one ([array count]==1), class B view is displayed, presenting an UITableView, wich data is populated with the array. Everything works perfectly for the fierst message.

A second message arrives. The new message is added to the array (that is done in class A) but, the table is not populated. How could i achieve that goal?

The code:

Class A

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:msg forKey:@"msg"];
[m setObject:from forKey:@"sender"];
[messarray addObject:m];


[self newMessageReceived:messarray];
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
NSLog(@"Method xmppStreamDidAuthenticate");
[self goOnline];
}

//- (void) newMessageReceived:(NSMutableDictionary *)message
-(void) newMessageReceived:(NSMutableArray *)messarraylocal
{
General *general = [General sharedManager];//An static class to store settings like username, and other things that must be available in the hole app.
general.numcels++;
general.messarray=messarraylocal;
  // NSString *firstmessage=[message objectForKey:@"msg"];
//NSString *from=[message objectForKey:@"sender"];    
//NSArray *listItems = [from componentsSeparatedByString:@"@"];
//NSString *fromsplit=[listItems objectAtIndex:0];
//general.firstmess=firstmessage;
//general.firstfrom=fromsplit;




if ([messarraylocal count]==1){
ChatViewController *cvc=[[ChatViewController alloc]initWithNibName:@"Chat" bundle:nil];

[[self navigationController]pushViewController:cvc animated:YES];

}
}

Class B:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
General *general = [General sharedManager];
NSLog(@"We are in numberOfRowsInSection, de ChatViewController");
return general.messarray.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
General *general = [General sharedManager];

NSLog(@"We are in cellForRowAtIndexPath de ChatViewController");
//UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
//if(!cell){
messarray=general.messarray;
for (int i=0;i<messarray.count; i++)
{
    diccio=[messarray objectAtIndex:i];
    general.firstmess=[diccio objectForKey:@"msg"];
    general.firstfrom=[diccio objectForKey:@"sender"];

UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
//}
text=general.firstmess;
remite=general.firstfrom;
[[cell textLabel]setText:remite];
[[cell detailTextLabel] setText:text];

return cell;

}
}

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
NSLog(@"We are in didReceiveMessage of ChatViewController");
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:msg forKey:@"msg"];
[m setObject:from forKey:@"sender"];


[self newMessageReceived:m];
}

- (void) newMessageReceived: (NSMutableDictionary *) message// this method is NEVER called
{
General *general = [General sharedManager];
general.numcels++;
NSLog(@"numcels vale %i", general.numcels);
NSString *firstmessage=[message objectForKey:@"msg"];
NSString *from=[message objectForKey:@"sender"];    
NSArray *listItems = [from componentsSeparatedByString:@"@"];
NSString *fromsplit=[listItems objectAtIndex:0];
general.firstmess=firstmessage;
general.firstfrom=fromsplit;
NSLog(@"Message receive in ChatViewController: %@ from %@", [message objectForKey:@"msg"], fromsplit);

}

In the method didViewLoad of class B, i do:

table.delegate=self;
table.dataSource=self;

But with no result. I know that [table reload] could work but… where can i do it?

Anybody can help me? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:37:06+00:00Added an answer on June 6, 2026 at 2:37 pm

    In class A newMessageReceived after you add the new message to the global messarray

    You will need to fire an NSNotification

    -(void) newMessageReceived:(NSMutableArray *)messarraylocal
    {
        //in addition to your normal code, add the following
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NewMessage" object:nil];
    }
    

    Now in Class B register for observation

    - (void) viewDidLoad
    {
        //In addition to your normal code here add the following
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMessage) name:@"NewMessage" object:nil];
    }
    
    //Add this method that will call reload data
    - (void) newMessage
    {
        [table reloadData];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which has two HashSet<String> collections as private members. Other classes
I have two classes, derived from a common class. The common class has a
Let's say I have two classes. Each class has one parameter. Parameter of first
I have two classes, WebServiceRequest and OrderRequest. Each class has properties. OrderRequest inherits from
I have two classes: Action class, that has a method for executing VBScript files,
I have two classes, Class A and Class B. Class A has a method
So let's say I have two classes that inherit a base class that has
I need to have two classes, one class has two methods each of which
I have two classes defined in different h files and each class has some
I have two classes. First class has TabPage control. I want to change layout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.