我使用 XCode4 和 Storyboard 创建了一个新的 iPad 应用程序。我有几 (5) 个不同的 View Controller 。我创建了一个类 (HomeViewClass),并采用其中一个 View Controller 并将该类更改为“HomeViewClass”,并将此代码放入 viewDidLoad 方法中。我最初在一个 Non-Storyboard 项目中使用它,它在那里工作得很好......(它在指示的地方循环)。
allButtons = [NSMutableArray array];
for(int k = 40, m = 0; m < 6; m++, k+= 42) // this controls the vertical distance between rows
for(int i = 0, j=182; i < 7; i++, j+=82) { // this controls the size (j) and horizontal distance
calendarButton *cb= [calendarButton buttonWithType:UIButtonTypeCustom];
[cb makeFrame:CGRectMake(j, k, 80, 40) number: allButtons.count+1 color:[UIColor orangeColor]];
// x y w h
[self.view addSubview:cb]; // <------------------- causing a viewDidLoad loop
[allButtons addObject:cb]; // put it in the array
}
我该如何解决这个问题?
最佳答案
您应该将 self.view 缓存在局部变量中,并检查您是否在方法的开头调用了 [super viewDidLoad]:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *rootView = self.view;
for(int k = 40, m = 0; m < 6; m++, k+= 42) {
for(int i = 0, j=182; i < 7; i++, j+=82) {
UIButton *cb = [UIButton buttonWithType:UIButtonTypeCustom];
[rootView addSubview:cb];
}
}
}
关于objective-c - 我的 "viewDidLoad"在循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9981253/