PDA

View Full Version : Other then an IBAction Refresh button, how to update the label?




jjgraz
Apr 29, 2009, 04:13 PM
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.



Niiro13
Apr 29, 2009, 05:43 PM
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).

jjgraz
Apr 29, 2009, 06:06 PM
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?

dejo
Apr 29, 2009, 06:23 PM
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.

jjgraz
Apr 29, 2009, 06:50 PM
Thanks for the reply Dejo.

Im Using viewWillAppear like this:


-(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.

BlackWolf
Apr 30, 2009, 12:29 AM
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 ... )

jjgraz
Apr 30, 2009, 01:55 AM
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..

BlackWolf
Apr 30, 2009, 11:05 AM
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?

jjgraz
Apr 30, 2009, 11:51 AM
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?


-(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.

dejo
Apr 30, 2009, 11:59 AM
I'd say you only need to do the update if it's not nil. Something along the lines of:
-(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.

jjgraz
Apr 30, 2009, 12:03 PM
I GOT IT. you were right....the if/else staement works....I had to put

blt_applicationAppDelegate *mainDelegate = (blt_applicationAppDelegate *)[[UIApplication sharedApplication]delegate];

in above the statement so it could review correctly.....New working code with no (null) below......


-(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

dejo
Apr 30, 2009, 12:10 PM
in above the statement so it could review correctly.....New working code with no (null) below......


-(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 if (mainDelegate.comet != nil) and skip the else-block!

jjgraz
Apr 30, 2009, 12:11 PM
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