我创建了一个名为 view2 的类,它是 UIView Controller 的子类,当我运行该程序时,计时器工作正常,但当它更改屏幕时,它只是变成黑色,而不是我在 view2 上的图片。这是我在打开 View 的 .m 上的代码,任何人都可以看到哪里出了问题吗?
- (void)viewDidLoad
{
randomMain = [NSTimer scheduledTimerWithTimeInterval:(2) target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
[super viewDidLoad];
}
-(void)onTimer{
view2 *second= [[view2 alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
最佳答案
您必须指定要加载的 xib 文件的名称(不带扩展名):
view2 *second = [[view2 alloc] initWithNibName:@"MyView2Xib" bundle:nil];
或者,如果您以编程方式创建 GUI,只需使用简单的 init
:
view2 *second = [[view2 alloc] init];
一些补充说明:
- 类名称应始终以大写字母开头,以区别于变量。
- 最好将父类(super class)名称的一部分包含在子类名称中。如果我看到一个名为
View2
的类,我会假设它是UIView
子类。我宁愿将其称为ViewController2
。 - 无需将间隔放在括号中。
NSTimer
的操作应该有一个参数(它本身)。
编辑:
使用 Storyboard,您必须手动执行在 2 个 View 之间设置的序列:
- (void)onTimer {
[self performSegueWithIdentifier:@"YourSegueIdentifier" sender:self];
}
关于objective-c - 不知道如何使用 NSTimer 更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13996143/