簡體   English   中英

從appdelegate中的viewcontroller調用方法不會更改屏幕上的文本

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

每次啟動應用程序時,我都想更改屏幕上的文本。 該方法被調用,但文本不會更改。

myAppDelegate.m

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

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

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

}

我在日志中得到“ methodA”。

試試這個代碼:

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

MainStoryboard替換為您的情節MainStoryboard名稱,並將id替換為您的視圖控制器ID。 您可以在情節提要中設置ID。 最后用您的視圖控制器替換yourViewController 。(不要忘記導入它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM