I have a class called RecordViewFooter
where i create a UIButton
called microphoneButton
. I add as a subview
and it looks beautiful.
However, when this button is selected i need to add something as a subview
to my ViewController
. So i did this in my ViewController
:
RecordViewFooter().microphoneButton.addTarget(self, action: "microphoneButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)
}
func microphoneButtonPressed(){
print("microphone button pressed")
volumeAdjuster = VolumeAdjusterView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
self.view.addSubview(volumeAdjuster!)
}
I can not figure out why it won't call the function when the button is selected. Any thoughts?
Try this modified code:
RecordViewFooter().microphoneButton.addTarget(self, action: "microphoneButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
func microphoneButtonPressed(button:UIButton){
print("microphone button pressed")
volumeAdjuster = VolumeAdjusterView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
self.view.addSubview(volumeAdjuster!)
}
You can solved that problem by implementing following solutions :
First Prepare object of RecordViewFooter class. like here i create object of RecordViewFooter() class.
var popViewObj = UINib(nibName: "RecordViewFooter", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! RecordViewFooter
now Write this code after subview done.
popViewObj.microphoneButton.addTarget(self, action: "microphoneButtonPressed:",forControlEvents: UIControlEvents.TouchUpInside)
Now Add Selected button Event in your code:
func microphoneButtonPressed(sender: UIButton) { print("microphone button pressed") // Do Whatever u want... }
it will work fine.. i hope u will done with this code.
I got it to work. I had to create an object from the class that i want to reference the global variable. Then do this:
objectFromClass.globalVariable.addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.