简体   繁体   English

从appdelegate中的viewcontroller调用方法不会更改屏幕上的文本

[英]Calling a method from viewcontroller in the appdelegate does not change the text on the Screen

Every time when I launch the app, I want to change the text on the screen. 每次启动应用程序时,我都想更改屏幕上的文本。 The method is called but text does not change. 该方法被调用,但文本不会更改。

myAppDelegate.m myAppDelegate.m

- (void)applicationDidBecomeActive:(UIApplication *)application
{

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:-1];
    viewController *object = [[viewController alloc] init];
    [object methodA];
}

viewController.m viewController.m

-(void)methodA{
    //Create an URL pointing to your plist
    NSURL *plistURL = [[NSBundle mainBundle] URLForResource: @"myPlist" withExtension: @"plist"];
    //Read the plist from the application bundle into an array object.
    NSArray *stringsArray = [NSArray arrayWithContentsOfURL: plistURL];
    //Create a random index into the array
    NSUInteger randomIndex = arc4random_uniform(stringsArray.count);
    //Fetch the string at that random index
    NSString *randomString = stringsArray[randomIndex];
    //Set some label's text to the random string.
    _myLabel.text = randomString;
    NSLog(@"methodA");

} }

I get "methodA" in my log. 我在日志中得到“ methodA”。

Try this code: 试试这个代码:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
yourViewController *object = [storyboard instantiateViewControllerWithIdentifier:@"id"];
[object methodA];

Replace MainStoryboard with your storyboard name and id with your view controller id. MainStoryboard替换为您的情节MainStoryboard名称,并将id替换为您的视图控制器ID。 You can set the id in your storyboard. 您可以在情节提要中设置ID。 Last replace yourViewController with your view controller.(Do not forget to import it). 最后用您的视图控制器替换yourViewController 。(不要忘记导入它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM