Other then an IBAction Refresh button, how to update the label?

Discussion in 'iOS Programming' started by jjgraz, Apr 29, 2009.

  1. macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #1
    I have a couple viewcontrollers which are passing a variable between them using the appDelegate. example: User goes to tab 3 on TabBar, clicks a button, which puts them back into the Tab 1 with the data to be loaded into an existing Label.....I want it to auto update the label......Right now I have a button there IBAction Refresh, which updates the label once pressed....How can I avoid using the button, and just have it update?

    Thank you.
     
  2. macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #2
    Prolly not the best way in terms of memory but you could create a separate NSThread and have it constantly check (or a timer that checks every few seconds) for the data (like an infinite loop).
     
  3. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #3
    I appreciate your reply.
    I was thinking there would be a method somewhat like (ViewDidLoad) only it runs every time the view is displayed.....Is this not the case?
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    viewDidLoad is not run every time the view is displayed, unless you are unloading between viewings. Anyways, I would suggest updating the label in the viewWillAppear or viewDidAppear methods.
     
  5. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #5
    Thanks for the reply Dejo.

    Im Using viewWillAppear like this:

    Code:
    -(void) viewWillAppear:animated {
    	blt_applicationAppDelegate *mainDelegate = (blt_applicationAppDelegate *)[[UIApplication sharedApplication]delegate];
    	
    	NSString* astring = [NSString stringWithFormat:@"%@", mainDelegate.sli];
    	
    	bltText.text = astring;
    
    }
    
    problem with this is because this particular view is seen before the view transfering the data....the label reads (null).....and when i transfer the data over it doesn't update beacuse it already reads (null)...
    How would I either stop it from reading (null), or have it reload/refresh. I prefer it didn't say (null)....Thank you.
     
  6. macrumors regular

    Joined:
    Apr 9, 2009
    #6
    couldn't you, in the viewController that SENDS that data, simply send the data, and after that invoke the method of your target view controller that takes care of changing the label and then displaying the view controller (or first displaying it, then sending the data, then invoking a method ... )
     
  7. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #7
    Right that works. Now, I got it working by sending the data through appDelegate and into second viewcontroller, and the method I'm using is viewDidAppear in secondViewController.............I still have the problem with this method......Because the view is seen prior to the data being sent........the value in label reads (null) up to the point in which data is sent.....then it reads correctly........How do Invoke this method so it only displays after the data is sent?......I'm sure you are going to say, I need a custom method......Can you please provide an example....Not sure how to do this one.....I appreciate it very much..
     
  8. macrumors regular

    Joined:
    Apr 9, 2009
    #8
    well, if I understood you right ... couldn't you just check if the value you write into the label exists? and if it doesn't (==null) you don't write into the label?
     
  9. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #9
    Here is what it looks like.............This works by the way......It's still showing (null) though, before data is sent. How do I say don't show null in the if statement?

    Code:
    -(void) viewDidAppear:animated {
    	if (bltText.text == nil)
        {
    	}
    	else 
    	{
    		blt_applicationAppDelegate *mainDelegate = (blt_applicationAppDelegate *)[[UIApplication sharedApplication]delegate];
    		
    		NSString* shonk = [NSString stringWithFormat:@"%@", mainDelegate.comet];
    		bltText.text = shonk;		
    	}	
    }
    
    Thanks for the help. It is much appreciated.
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    I'd say you only need to do the update if it's not nil. Something along the lines of:
    Code:
    -(void) viewDidAppear:animated {
    		blt_applicationAppDelegate *mainDelegate = (blt_applicationAppDelegate *)[[UIApplication sharedApplication]delegate];
    	if (mainDelegate.comet != nil)
     	{
    		
    		NSString* shonk = [NSString stringWithFormat:@"%@", mainDelegate.comet];
    		bltText.text = shonk;		
    	}	
    }
    P.S. Depending on how comet is defined you might not even need to create a new shonk string. I.E. bltText.text = mainDelegate.comet; might be sufficient.
     
  11. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #11
    I GOT IT. you were right....the if/else staement works....I had to put
    Code:
    blt_applicationAppDelegate *mainDelegate = (blt_applicationAppDelegate *)[[UIApplication sharedApplication]delegate];
    
    in above the statement so it could review correctly.....New working code with no (null) below......

    Code:
    -(void) viewDidAppear:animated {
    	blt_applicationAppDelegate *mainDelegate = (blt_applicationAppDelegate *)[[UIApplication sharedApplication]delegate];
    	if (mainDelegate.comet == nil)
        {		
    	}
    	else 
    	{
    		blt_applicationAppDelegate *mainDelegate = (blt_applicationAppDelegate *)[[UIApplication sharedApplication]delegate];
    		
    		NSString* shonk = [NSString stringWithFormat:@"%@", mainDelegate.comet];
    		bltText.text = shonk;	
    	}
    	
    }
    
    
    Thank you very much for your help and effort in getting this one sorted out. It really helped a ton. :D
     
  12. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    There's no need to redefine your mainDelegate variable within the else. Also, why have an empty if-block? Just do an
    Code:
    if (mainDelegate.comet != nil)
    and skip the else-block!
     
  13. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #13
    alright Dejo...your code a bit cleaner than my frankensteined ensemble....and works the just the same......Perfect! I really appreciate all your help.......Thank you. :D
     

Share This Page