Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
On my default navbar's rootview (a chatroom of sorts) I have a title bar, with a rightBarButton "compose", which pops up a uitextview and keyboard to enter a new chat message. After you hit "send", the message sends back to the server to store it in the db, that works fine.

When I close the uitextview and load the rootview back, I do:

[newMessageEntry resignFirstResponder];
[self loadView];

That does show the old chat messages successfully, but I'm not sure how to auto-refresh it so it picks up the message you just submitted. I'm thinking the "self loadView" doesn't really load up the view with all the code, because in that view's 'viewDidLoad' method, I start the code to load the messages from the server and display them, but that code is not running.

Am I making any sense? Hello? Is this thing on? :)
 

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
Hmmm

Not sure I follow...

I am executing this code from inside the rootviewcontroller for the navbar, so I tried:

[self setNeedsDisplay];

But that crashed the thing :)

Looking into setNeedsDisplay to see what it does...
 

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
Hmm...

Hmm, that didn't do it, although it DID mess up the scrolling on that page ;)

I appreciate the effort, I'm still poking around the docs looking for how to handle this!
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
setNeedsDisplay will mark your view for redrawing, which will call viewDidLoad (or loadView, depending how you've implemented things) which is the method you should be using to layout your view.

If you're getting strange behavior then it's most likely being caused by logic in one of those methods that doesn't belong (since it seems to be affecting more than the view layout). Try moving these bits into more appropriate methods (for instance your init method).

dejo's point is that calling setNeedsDisplay should re-layout your view and in so doing pick up the new messages so that they appear in the view :)

I'm thinking the "self loadView" doesn't really load up the view with all the code, because in that view's 'viewDidLoad' method, I start the code to load the messages from the server and display them, but that code is not running.

Only one of these methods will ever be called; loadView if you've implemented your view programmatically, viewDidLoad if you're working from a nib file.
 

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
Ahhh...

Ok, that DOES clear up my confusion around vewDidLoad and loadView. I am building the layout programatically, so I am only using loadView now, and after "send" is pressed, I execute:

Code:
	[newMessageEntry resignFirstResponder]; 
	[self.view setNeedsDisplay];
	[self loadView];

But, same issue, is doesn't pick up the new data? Here's my loadView:

Code:
- (void)loadView {
	[super loadView];
	
	if ([messages count] == 0) {
		NSString * path = @"http://localhost:8081/convo";
		[self parseXMLFileAtURL:path];
	}

	self.navigationItem.title=@"Chat";
	UIBarButtonItem *newMessageButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(newMsgClicked:)] autorelease];
	
	self.navigationItem.rightBarButtonItem = newMessageButton;
}

Many thanks fellas for helping me out. I'm very much a novice with the iphone sdk, but having fun learning! Thanks again!
 

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
Wondering..

Could it be that... re-layouting that view happens much faster than sending a message to the server, storing it, and retreiving it?

Maybe a race condition going on here?

If so, is it possible to add a few second delay here to test it out?
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
Depends if parseXMLFileAtURL is synchronous or not. Guessing not, since it would block the UI otherwise, so could well be that it's a race condition.
 

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
hmm

* goes looking through docs for how to display a 2 second "loading" delay box with nice little HUD style and spinner :)
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Could it be that... re-layouting that view happens much faster than sending a message to the server, storing it, and retreiving it?
I'd suggest not invoking the setNeedsDisplay until after the message has been retrieved, if you are relying on the message received from the server to provide the content for the updated view.
 

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
bleh

To test, tried:

Code:
	[newMessageEntry resignFirstResponder]; 
	[NSThread sleepForTimeInterval:5.0];
	[self.view setNeedsDisplay];
	[self loadView];

It did delay for 5 seconds there, but when the view came up, the message was not there. The message DOES, however, make it to the db within those 5 seconds, so I guess something in my loadView isn't doing what I think it is?

Going to try and add some log messages to better track the data flow going on here...
 

UberMonjie

macrumors newbie
Original poster
Feb 11, 2009
18
0
EEk!

I'm an idiot.

I had a conditional in my that I dropped in for debugging a while back in loadView that was omitting the stuff I need to happen.

Your original suggestions of using setNeedsDisplay worked as described. Sorry, and thank you!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.