Refreshing a navbar view

Discussion in 'iOS Programming' started by UberMonjie, Feb 23, 2009.

  1. macrumors newbie

    Joined:
    Feb 11, 2009
    #1
    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? :)
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  3. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #3
    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...
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Sorry about that. What I meant was:
    [rootViewController.view setNeedsDisplay] or something along those lines.
     
  5. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #5
    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!
     
  6. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #6
    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 :)

    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.
     
  7. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #7
    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!
     
  8. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #8
    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?
     
  9. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #9
    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.
     
  10. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #10
    hmm

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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    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.
     
  12. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #12
    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...
     
  13. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #13
    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!
     

Share This Page