[英]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.