我在 appdelegate 中调用这个方法
[[[ViewController alloc] init] performSelectorOnMainThread:@selector(callMethod) withObject:nil waitUntilDone:NO];
ViewController.h 中的方法
@interface ViewController : UIViewController
-(void)callMethod;
@end
ViewController.m 中的方法
-(void)callMethod{
NSLog(@"Method was call.....");
UIView *myView = [[UIView alloc] initWithFrame:self.view.bounds];
myView.backgroundColor = [UIColor colorWithRed:0.113725F green:0.615686F blue:0.411765F alpha:1.0F];
[self.view addSubview:myView];
}
它可以在控制台中打印,但 addSubView 不起作用。 如何使 addSubView 工作。 感谢。 :)
最佳答案
您需要先将这个新分配的 View Controller [[ViewController alloc] init]
push
到堆栈上,以便查看它。或者,如果此 Controller 已被推送 或选项卡,您不必使用 alloc
重新分配它关键字。
在 appdelegate 中尝试这样:
// Get the visible viewController
ViewController *vc = [self.navigationController visibleViewController];
[vc performSelectorOnMainThread:@selector(callMethod) withObject:nil waitUntilDone:NO];
关于ios - 从 appdelegate 调用方法时无法添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308286/