How to change label in ViewController when app enters the foreground

╄→гoц情女王★ 提交于 2019-12-11 09:51:25

问题


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


回答1:


Is label hooked up in Interface Builder?




回答2:


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;
        }
    }



回答3:


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);

}




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/6268094/how-to-change-label-in-viewcontroller-when-app-enters-the-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!