PDA

View Full Version : Refreshing a navbar view




UberMonjie
Feb 23, 2009, 10:30 AM
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? :)



dejo
Feb 23, 2009, 10:32 AM
[loadView setNeedsDisplay] ?

UberMonjie
Feb 23, 2009, 10:37 AM
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...

dejo
Feb 23, 2009, 10:43 AM
Sorry about that. What I meant was:
[rootViewController.view setNeedsDisplay] or something along those lines.

UberMonjie
Feb 23, 2009, 10:55 AM
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
Feb 23, 2009, 11:02 AM
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
Feb 23, 2009, 11:14 AM
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:


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


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


- (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
Feb 23, 2009, 11:45 AM
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
Feb 23, 2009, 11:48 AM
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
Feb 23, 2009, 11:50 AM
* goes looking through docs for how to display a 2 second "loading" delay box with nice little HUD style and spinner :)

dejo
Feb 23, 2009, 11:51 AM
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
Feb 23, 2009, 11:56 AM
To test, tried:


[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
Feb 23, 2009, 12:03 PM
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!