我创建了一个按钮开关盒。按下该按钮,当再次单击该按钮时,图像被添加到子视图中,如果再次单击该按钮,它将从超级视图中删除它,它将再次添加它们等。
var buttonState: Int = 0;
func addfriend(sender: UIButton){
switch(buttonState){
case 0:
self.view.addSubview(cameramethod)
self.view.addSubview(searchmethod)
self.view.addSubview(nearbymethod)
buttonState = 1;
break;
case 1:
cameramethod.removeFromSuperview()
searchmethod.removeFromSuperview()
nearbymethod.removeFromSuperview()
default: break;
}
}
答案 0 :(得分:1)
你想这样吗?
func addfriend(sender: UIButton){
switch(buttonState){
case 0:
self.view.addSubview(cameramethod)
self.view.addSubview(searchmethod)
self.view.addSubview(nearbymethod)
buttonState = 1;
break;
case 1:
cameramethod.removeFromSuperview()
searchmethod.removeFromSuperview()
nearbymethod.removeFromSuperview()
buttonState = 0;
break;
default: break;
}
}
答案 1 :(得分:0)
我认为您应该使用布尔类型而不是整数来保存按钮状态:
var buttonDisplay = false
func addfriend(sender: UIButton){
// toggle your button state when pressed
buttonDisplay = !buttonDisplay
// check if true
if buttonDisplay {
// case positive add your subviews
view.addSubview(cameramethod)
view.addSubview(searchmethod)
view.addSubview(nearbymethod)
else {
// otherwise remove them
cameramethod.removeFromSuperview()
searchmethod.removeFromSuperview()
nearbymethod.removeFromSuperview()
}
}
答案 2 :(得分:0)
这样做,玩按钮的标签值。
func addfriend(sender: UIButton){
switch(sender.tag){
case 0:
self.view.addSubview(cameramethod)
self.view.addSubview(searchmethod)
self.view.addSubview(nearbymethod)
sender.tag = 1;
break;
case 1:
cameramethod.removeFromSuperview()
searchmethod.removeFromSuperview()
nearbymethod.removeFromSuperview()
sender.tag = 0;
default: break;
}