目标: 我有一个专为儿童设计的游戏。所有级别都有单词族,每个单词族都有一定数量的相关活动。
每个级别都有我称之为“菜单” - 当孩子到达此菜单时,他们将点击“播放”按钮,将他们带到下一个活动。完成此活动后,他们不再能够返回到它并且星形出现在按钮所用的位置。如果活动尚未解锁,则孩子将看不到该活动的按钮。
这是一个直观的表示:
我的更新代码:(由于DonMag指出的错误而更改后)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Get place entity for current level
entity = [[UserEntity sharedEntity] placeEntityForLevel:[LevelObject currentLevel]];
NSInteger level = [LevelObject currentLevel] + 1;
// Load menu view corresponding to current level from its NIB.
NSString *nibName = [NSString stringWithFormat:@"%li",(long)level];
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
levelView = [nibViews firstObject];
[levelView setFrame:self.view.bounds];
[self.view addSubview:levelView];
[self setMenuView:levelView];
// Hide all menu item views.
int i = 1;
for (UIButton *view in levelView.subviews) {
if ([view isKindOfClass:[UIButton class]])
{
view.hidden = true;
view.userInteractionEnabled = false;
// Populate star image views with images.
NSString *imagePath = [NSString stringWithFormat:@"pictures/%dstar.png",i];
[view setBackgroundImage:[UIImage imageNamed:imagePath] forState:UIControlStateDisabled];
i++;
i = i * (i <= 5);
}
}
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Get an array of all menu item views for activities up to, and including, the current activity.
NSInteger placeTag = entity.familyNum * 10 + entity.activityNum;
NSPredicate *filteringPredicate = [NSPredicate predicateWithBlock:^BOOL(id obj1,NSDictionary *dictionary) {
return [obj1 isKindOfClass:[UIButton class]];
}];
NSArray *filteredViews = [levelView.subviews filteredArrayUsingPredicate:filteringPredicate];
// Loop through all views for activities up to, and including, current activity.
for (int representedFamily = 0; representedFamily <= entity.familyNum; representedFamily++) {
for (int representedActivity = 0; representedActivity <= entity.activityNum; representedActivity++)
{
NSInteger thisViewTag = representedFamily * 10 + representedActivity; // Tag of the view corresponding to represented family and activity.
// Find view with thisViewTag
UIButton *thisView = [[filteredViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(MenuItemView *view, NSDictionary *dictionary) {
return view.tag == thisViewTag;
}]] firstObject];
NSAssert(thisView != levelView, @"levelView is thisView - change levelView's tag");
thisView.hidden = false;
if (thisViewTag == placeTag) {
// Current activity
currentActivityTag = thisViewTag;
thisView.enabled = true;
thisView.hidden = false;
[thisView addTarget:self action:@selector(activityButtonPressed:) forControlEvents:UIControlEventTouchDown];
}
else
{
// Past activity
thisView.enabled = false;
// Assert that star is not hidden.
assert(thisView.hidden == false);
}
}
}
}
问题:
即使在活动完成后,某些明星也不一定会出现。
我尝试解决方案:
我已经尝试将代码放在viewWillAppear中。 我已经重新安装了Xcode并运行了“Clean Build Folder”
可能不是解决方案: 创建familyNum和activityNum属性,并使用这些属性而不是标签 - 最多有3个活动。
我想到的其他人: 只需绕过UIViews并直接绘制到屏幕上(ew)
更新(6月6日):我在“if thisViewTag == placeTag”中添加了一个断点,另一个在“else”语句中添加了断点;这些导致应用程序停止所有星星,除了那些没有显示的星星。我的调试器还显示我的filteredArray具有它需要的所有按钮。
答案 0 :(得分:0)
我修好了!
问题是双重的。第一:
1)我有比星星更多的子视图。在一定次数的迭代后重置“i”计数器有帮助。 2)表示活动的我的for循环未能解决表示活动&gt;的情况。 entity.activityNum但entity.familyNum&gt; representedFamily。