我想检查我的视图是否已添加到视图中,以便用户无法重复将新的UIViewController添加到视图中。
if (!self.fmovc)
{
self.fmovc = [[FMOVC alloc] initWithNibName: @"FMOVC" bundle:nil];
}
BOOL viewAlreadyDisplayed = NO;
for (UIView * b in self.view.subviews)
{
if ([b isKindOfClass:[FMOVC class]])
{
viewAlreadyDisplayed = YES;
}
}
if (!viewAlreadyDisplayed)
{
[self.view addSubview:self.fmovc.view];
}
这不起作用,因为它永远不会触发
viewAlreadyDisplayed = YES;
查找您的视图是否已经显示的正确方法是什么?
谢谢, -code
答案 0 :(得分:0)
在添加之前只需删除ifItFromSuperView ...它可以解决你的问题
[self.fmovc.view removeFromSuperview];
[self.view addSubview:self.fmovc.view];
答案 1 :(得分:0)
使UIView成员变量说明为View;
if(!thatView){ thatView = [[UIView alloc] init]; [self.view addSubView:thatView]; }
发布超级视图后,发布该视图&将thatView设为nil。
OR
始终检查&在分配thatView之前从superview中删除thatView。
希望这有助于!!!