Ios Swift:在子视图中隐藏按钮
我试图在启动函数时在子视图中隐藏UIButton。 我有多个具有该层次结构的视图:Ios Swift:在子视图中隐藏按钮,ios,button,swift,subview,Ios,Button,Swift,Subview,我试图在启动函数时在子视图中隐藏UIButton。 我有多个具有该层次结构的视图: var takePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton takePhotoButton.addTarget(self, action:"takePhoto", forControlEvents:UIControlEvents.TouchUpInside)
var takePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
takePhotoButton.addTarget(self, action:"takePhoto", forControlEvents:UIControlEvents.TouchUpInside)
var savePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
let view2:UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
self.view.addSubview(photoMask)
photoMask.addSubview(view2)
view2.addSubview(takePhotoButton)
当以下func被激活时,我想隐藏takePhotoButton,我该怎么做
func takePhoto(takePhotoButton: UIButton!) {
takePhotoButton.hidden = true
}
如果要保留对UIButton的引用,必须将其作为类的属性。然后您可以使用
self.takePhotoButton
来访问它,并为button创建一个参考变量。通过此命令将其隐藏属性设置为true
self.yourReferenceVariable.hidden = true
使用takePhoto:作为添加目标时的选择器,调用该方法时将传递该按钮
var takePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
takePhotoButton.addTarget(self, action:"takePhoto:", forControlEvents:UIControlEvents.TouchUpInside)
var savePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
let view2:UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
self.view.addSubview(photoMask)
photoMask.addSubview(view2)
view2.addSubview(takePhotoButton)
然后,在方法中隐藏按钮
func takePhoto(takePhotoButton: UIButton!) {
takePhotoButton.hidden = true
}
在Swift 3.0中,您需要使用
ishiden
而不是hidden
subview.isHidden = true
特别是在这种情况下,
self.subview.isHidden = true
好的,当我添加按钮作为选择器时,我可以在takePhoto方法中访问它,但当我在应用程序中按下按钮时,我有一个新的错误:无法识别的选择器发送到实例是的,你也应该添加我上面添加的方法,takePhoto(takePhotoButton:UIButton!)我做到了,查看我编辑的帖子,它现在是我的实际代码。但是您没有将选择器设置为takePhoto:,takePhotoButton.addTarget(self,action:,takePhoto:,forControlEvents:UIControlEvents.TouchUpInside)嗨,如果按钮不是选择器,则无法访问它。如果是,我的应用程序将崩溃,并出现以下错误:无法识别的选择器已发送到实例