UIButton Action not working in swift - ios

I have two classes UIVIewController and UITableviewcell. In UITableviewcell, i have a button and the button action should be in UIViewController. How can i pass a class and set delegate in button action.
i tried this code,
//UIVIewController
cell.createButton(self)
func buttonClick(object:AnyObject){
println("Button Clicked")
}
//UITableViewCell
func createButton(delegate:AnyObject){
//button created.
button.addTarget(delegate, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
}
But the above code is not working. 'buttonClick' function is not calling

I had the same problem a while ago, trying to put self in place of "delegated".
otherwise try to take a look at this question:
link
I hope to be helped.

Related

Swift custom button class

I'm trying to create a custom class that creates a button. I'm having trouble adding a target to that button inside it's class. This is my code
class SelectButton{
var button:UIButton = UIButton()
init(button_frame: CGRect, button_title: String, connected: [UIButton]?){
self.button.frame = button_frame
self.button.setTitle(button_title, for: UIControlState.normal)
self.button.addTarget(self, action:#selector(self.buttonPressed), for: .touchUpInside)
}
func construct() -> UIButton {
return self.button
}
#objc func buttonPressed() {
print("Button Clicked")
}
}
The problem is that I can't connect an action on button click. This works if it's used outside my class but not inside.
Usage of the class
let test = SelectButton(button_frame: CGRect(x:50, y:50, width: 250, height:150), button_title: "Test button", connected: nil).construct()
self.view.addSubview(test)
When someone taps the button, usually you want something to happen somewhere else in your app (like in one of your view controllers or in some other UI element). The way the IBAction is set up right now, you have it so that something will trigger or happen within the button itself when someone taps on it. If you want to handle a button tap programmatically instead of ctrl dragging from the button into the view controller, you can do it this way if you prefer. First, add this code into the view controller:
#IBAction func buttonPressed(sender: UIButton) {
}
Then you can either add the selector programmatically by adding this method into your view controller:
myButton.addTarget(self, action:self.buttonPressed(sender), for: .touchUpInside)
Or by going to the connections inspector and dragging from the touch up inside over to the IBAction dot in your view controller code. Also, as someone else pointed out in the comments you should make your button inherit from UIButton by adding this to your class declaration:
class SelectButton: UIButton {
. . .
}
Nothing is holding a strong reference to your SelectButton instance, so as soon as the function that creates test exits, that instance is released.
The button itself is retained because you have added it as a subview. Therefore, it is still visible but there is no longer an object to respond to the action.
You either need to use an instance property rather than a local variable for test, or, preferably have SelectButton inherit directly from UIButton

ImageView and Button inside CollectionView

I am building my first iOS-App using Swift and I am stuck.
I have a CollectionView and inside one of the Cells there is a button and an imageView.
What should happen is: I click the button and the imageView shows another picture.
I already created a CellSubClass but I just don't get how to use it.
Can anyone PLEASE help me?
Write an action method for your button inside your cellClass like this:
func changeImage
{
var image: UIImage = UIImage(named: "your image name")!
yourImageView.image = image
}
Don't forget to "assign" this method to your button either in initialization method for the cell (awakeFromNib is also called when cell is started or oyu could write your own custom init method). You can also do it by using IBAction from storyboard (Simple dragging and clicking).
But programmatically it is done Like this if you use the awakeFromNib approach:
override func awakeFromNib() {
super.awakeFromNib()
yourButton.addTarget(self, action: "changeImage", forControlEvents: UIControlEvents.TouchUpInside)
}
To learn how to create IBAction using storyboard, read here.

How to trigger UIButton target event before parent TapGestureRecognizer

It's a simple question,
I have a UIButton with a target action
addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
func buttonPressed()
{
self.selected = !self.selected
}
This button is the child of a parent UIView
containerView.addSubview(button)
I add a UITapGestureRecognizer to the container
let tapG = UITapGestureRecognizer(target: self, action: "toggleView:")
containerView.addGestureRecognizer(tapG)
in func toggleView(gest:UIGestureRecognizer) I check the status of my button.
The problem is that in toggleView(), the status of the button has not been set... YET!
EDIT
if I do
print(button.selected)
I only get the button value BEFORE the touch event, not the new one.
any idea?
Try to change
func toggleView(gest:UIGestureRecognizer)
to
func toggleView(gest:UITapGestureRecognizer)
Hope it helps!
I don't exactly get what you are trying to accomplish, If you want to perform certain action on button click and another action when tapped outside in containerView then you can do
-(void)toggleView:(UITapGestureRecognizer *) gest{
CGPoint tapLocation = [gest loactionInView:containerView];
if(CGRectContainsPoint(button.frame, tapLocation)){
// Tapped on Button
}
else{
// tapped outside in containerView
}
}
Note: As am not learning swift, i gave snippet in Objective-C. you should be able to translate it into swift easily :)

Having trouble targeting a DesignableView in a UITableViewCell upon tapping it in order to change

and thanks in advance for taking the time to help.
Inside my CellForRowAtIndexPath, I have the following line:
cell.timeView.addTarget(self, action: "ButtonDidPress:", forControlEvents: UIControlEvents.TouchUpInside)
and my selector function is:
func ButtonDidPress (sender: DesignableView!){
let view:DesignableView = sender
cell.timeView.shadowColor = UIColor.yellowColor()
table.reloadData()
}
and the error i get is:
unrecognized selector sent to instance
I'm thinking that perhaps one can't send a View as a selector (am I using the correct terminology?), but how else can I target that particular view in that cell?
UPDATE:
I also tried using gestureRecognizer instead:
var tap = UIGestureRecognizer(target: self, action: Selector( "viewDidTap:"))
cell.timeView.addGestureRecognizer(tap)
and
func viewDidTap (sender: DesignableView!){
but I got the same error here.
Thanks!
There's a couple of strange things happening in your code. It seems you want to change the shadowColor property of timeView when a user touch it, right?
Two possible solutions are:
(This one is IMO the better one) Change DesignableView to inherit from UIButton. Then you can set:
timeView.addTarget(self, action: "ButtonDidPress:", forControlEvents: .TouchUpInside). Make sure you set it just once for each cell. Otherwise you will get multiple calls on one tap.
Use UITapGestureRecognizer, but you should put it in your UITableViewCell subclass, not to the view controller. Also, the sender in viewDidTap is not the view itself, but the recognizer. So the method will go like this:
func viewDidTap(sender: UITapGestureRecognizer) {
let location = sender.locationInView(sender.view)
if timeView.hitTest(location, withEvent: nil) == timeView {
timeView.shadowColor = UIColor.yellowColor()
// table.reloadData() - you don't need to reload the table
}
}

Xcode: Sent Events Disappear upon changing UIButton class to custom class

I have created a custom UIButton class:
class MyButton: UIButton {
}
Then I added a UIButton to my storyboard and changed its class to MyButton.
Now when I right-click-drag the button to code, I am able to add Outlet but not Action. "Sent Events" section also disappears in property inspector. What could I be missing?
The only trick I've discovered until now is to change the button class back to UIButton, add the requested actions and then change back the class to MyButton :/
The bug is still there.
I was able to fix this by saving and restarting Xcode.
I know this problem is almost two years old, but I ran into it today. Restarting Xcode did not help. I noticed in some older .xib's, that there were some linked Sent Events, but all the rest of the touchDown etc. options were missing. However, after I hovered over the source on one of the remaining Sent Events, Xcode popped up a 'UIButton' is no longer a valid event source message. In this link, someone discovered that that problem is linked to an extension of UIButton. Sure enough, I refactored the UIButton extension we had to UIControl and all the Sent Event options returned.
Based on the OP's issue and what I read today, Apple is trying to tell us to leave their buttons alone. :)
you can use this code easy as pie
override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchDown)
self.addTarget(self, action: "buttonUnClicked:", forControlEvents: UIControlEvents.TouchUpInside)
self.addTarget(self, action: "buttonUnClicked:", forControlEvents: UIControlEvents.TouchUpOutside)
}
func buttonClicked(sender: UIButton){
self.backgroundColor = UIColor.redColor()
}
func buttonUnClicked(sender: UIButton){
self.backgroundColor = UIColor.blackColor()
}

Resources