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

jjgraz

macrumors regular
Original poster
Feb 13, 2009
103
0
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.
 
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).
 
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?
 
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?
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.
 
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.
 
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 ... )
 
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..
 
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..

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?
 
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.
 
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.
 
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
 
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;	
	}
	
}
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!
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.