使用按钮操作从SuperView移除UiView移除
我对按钮的动作有问题。。。我不明白为什么事情不顺利,我想问你是否可以帮我理解错误 在我的viewController中,我有一个处于选定模式的按钮,在另一个UIView中添加一个UIView,但当按钮被引入时,如果未选中,则应删除先前输入的视图,它将保留在那里,而不执行任何操作。。。我试过这样做,但我不明白我错在哪里使用按钮操作从SuperView移除UiView移除,uiview,uibutton,ios8,xcode6,Uiview,Uibutton,Ios8,Xcode6,我对按钮的动作有问题。。。我不明白为什么事情不顺利,我想问你是否可以帮我理解错误 在我的viewController中,我有一个处于选定模式的按钮,在另一个UIView中添加一个UIView,但当按钮被引入时,如果未选中,则应删除先前输入的视图,它将保留在那里,而不执行任何操作。。。我试过这样做,但我不明白我错在哪里 - (IBAction)shareActive:(id)sender { UIView *checkActive = [[UIView alloc]init];
- (IBAction)shareActive:(id)sender {
UIView *checkActive = [[UIView alloc]init];
checkActive.frame =CGRectMake(2, 2, 11, 11);
checkActive.layer.masksToBounds = YES;
checkActive.layer.cornerRadius = 5.5f;
checkActive.backgroundColor = [UIColor greenColor];
if (!self.condividiButton.selected) {
self.condividiButton.selected = YES;
[self.checkCondividi addSubview:checkActive];
NSLog(@"attivo");
}else {
self.condividiButton.selected = NO;
[checkActive removeFromSuperview];
NSLog(@"disattivo");
}
}
以下是许多可能的解决方案之一 我相信其他人也会对如何解决这个问题给出不同的想法
- (IBAction)buttonShareTouchedUpInside:(UIButton *)sender {
if (!self.condividiButton.selected) {
UIView *checkActive = [[UIView alloc]init];
checkActive.tag = 121212;
checkActive.frame =CGRectMake(2, 2, 11, 11);
checkActive.layer.masksToBounds = YES;
checkActive.layer.cornerRadius = 5.5f;
checkActive.backgroundColor = [UIColor greenColor];
self.condividiButton.selected = YES;
[self.checkCondividi addSubview:checkActive];
} else {
self.condividiButton.selected = NO;
[self.checkCondividi.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
if (obj.tag == 121212) {
[obj removeFromSuperview];
}
}];
}
}
注意:原来的问题可以通过许多其他可能更优雅的方式解决,但这里我不关心这些。调用此方法时,您总是创建一个新的
UIView
本地实例作为checkActive
。显然,该实例与您在上一次会话中添加到superview的实例完全不同–因此,当您要删除该实例时,您正在使用从未添加到任何superview的新实例,并且不会产生任何影响。事实上。。。我发现了错误。。。我在头文件中创建了一个属性,并在viewDidLoad中初始化视图,然后在视图中的按钮方法中,我指定了视图中的添加和删除,现在一切都正常了。。非常感谢您的帮助……:)