2

每次启动应用程序时,我都想更改屏幕上的文本。该方法被调用,但文本没有改变。

myAppDelegate.m

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

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

视图控制器.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”。

4

1 回答 1

0

试试这个代码:

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

替换MainStoryboard为您的故事板名称和id您的视图控制器 ID。您可以在情节提要中设置 id。最后替换yourViewController为您的视图控制器。(不要忘记导入它)。

于 2013-09-02T20:20:59.370 回答