开发者

How to change label in ViewController when app enters the foreground

开发者 https://www.devze.com 2023-03-10 20:41 出处:网络
i want to change a label in my view controller when the app enters the foreground....: SalaryAppV4AppDelegate.h

i want to change a label in my view controller when the app enters the foreground....:

SalaryAppV4AppDelegate.h

@interface SalaryAppV4AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

//----------------------------------------------    

    NSTimeInterval appEnteredBackground;

    NSTimeInterval appEnteredForeground;

    NSTimeInterval difference;

    NSString *times;

//-----------------------------------------------

SalaryAppV4AppDelegate.m

- (void)applicationWillEnterForeground:开发者_StackOverflow(UIApplication *)application

{


    //perform -(IBAction)DoThis

    FirstViewController* controller = [FirstViewController alloc];
    [controller DoThis]; 




    //releasing
    [dateFormatter release];





}

FirstViewController.m

-(IBAction) DoThis
{

    appDelegate = [[[UIApplication sharedApplication] delegate] retain];

    //This Doesn't Work :(
    label.text = @"IT Has Worked";


    //This Works
    NSLog(@"%@", appDelegate.times);

}
//--------------------------------------------------------

i just want the label.text ti change to anything but nothing in the viewcontroller changes...


Is label hooked up in Interface Builder?


First make sure that label is not nil. Second if your viewcontroller is in a navigation stack then you need to get your view Controller from navigation stack.

Here is a code snippet to get viewController from Navigation stack

HomeViewController *vController = nil;
    NSArray *vControllers = [self.navigationController viewControllers];
    for(UIViewController *v in vControllers) {
        if([v isKindOfClass:[HomeViewController class]]) {
            vController = (HomeViewController*)v;
            break;
        }
    }


RootViewController * controller = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];

-(IBAction) DoThis {

appDelegate = [[[UIApplication sharedApplication] delegate] retain];

change this to like with setText:

[label setText:@"IPhone/Ipad Chat Room"];
//This Works
NSLog(@"%@", appDelegate.times);

}


Inside your applicationWillEnterForeground: method, you are creating a new instance of FirstViewController and then calling your method DoThis: on it, which does change the label text. However, this controller is not a member object and the version of it that you see that does not update is probably in another code file somewhere - or is it in your app delegate too?

In short, although you do change the text it's not the correct object which is why you do not see any visible change, I reckon.


Using the method in the App delegate and a instance call to the View Controller

- (void)applicationWillEnterForeground:(UIApplication *)application {
   [ViewController.label setText:@"DA TEXT"]; //if the VC is a property.
   [[ViewController instance].label setText:@"DA TEXT"]; //if is not a property

}

If is not a property, you must set an instance class method at your VC

0

精彩评论

暂无评论...
验证码 换一张
取 消