UIButton cannot clicked after adding to another uiview - ios

I try to make the slideshow using uiscrollview, it will be triggered by slide event from uiscrollview.
Everything works fine except I try to add uibutton to the view, the button can be added and shown in the view but when I try to click the button, it does not triggered the function that I have stated in the addTarget function.
Anyone knows how to triggered the function assigned to the button?

First Create func:
func messagePrint(_ sender:UIButton) {
print("function called")
}
Then addTarget into UIButton:
btnDemo.addTarget(self, action: #selector(messagePrint(_:)), for: .touchUpInside)
In Above code btnDemo is UIButton Outlet.

Related

UIButton in UIStackView button click is not working

In UIViewController I have subview of UIView inside that I have a UIStackView like the below hierarchy:
UIView -> UIStackView -> UIView -> UIView -> UIButton
The UI which I design is working perfect but the button click is not working. I have created a IBAction but the click event is not working. I have checked the form nothing is helped me. Is anything should I need to do additionally?
How can I enable the button click event?
My button click Action
#IBAction func buttonClicked(_ sender: Any) {
print("Button Clicked")
}
My sample UI
Please check following -
User interaction of each superview.
IBAction for all buttons.
If you are still having issue then let me know. Happy coding
Check the Action connected to it and class of the object, if not rewire the thing or remove and drag a new object. Also check the constraints.

UIButton setImage didn't change the image of button immediately

I am using setImage() to set image on UIButton, but it doesn't change the image immediately. To show the image, I need to click the button or other button again. How to fix this problem?
block.setImage(UIImage(named: "Oimage.png"), for: UIControl.State.normal)
Problem is not how you set image, but where you set image. You probably set it inside IBAction for button so you will have to wait then button will be pressed.
One way is to set it inside viewDidLoad of your view controller
override func viewDidLoad() {
...
block.setImage(UIImage(named: "Oimage.png"), for: .normal)
}
... or if you're using storyboard, you can set it via storyboard. If you're creating your button programmatically, set it inside initializating lazy variable
You can set image in button action like this:
#IBAction buttonTapped(_ sender: UIButton) {
let testImage = UIImage(named:"imageName")
block.setImage(testImage, for: .normal)
}
just change Type of your button to custom

TouchDown from UIControl Never Called

I have changed my view to a UIControl in order to add a TouchDown recognizer (that way I can dismiss isEditing in my UITextView). I have also added an outlet to a function in my class. All my other outlets work, but when I touch down this never gets called. Could it be because my Control has many things layered on top of it? Any other idea why?
#IBAction func userTappedBackground(_ sender: Any) {
bioTextView.endEditing(true)
}

How can I call an action through UIButton variable

I have a UIButton in a prototype UItableviewcell that is made in a nib.
The UITableView is inside a UIViewController.
The requirement is: When I press the UIButton, a segue should be performed from Main UIViewController to another second UIViewController . I can access the UIButton IBOutlet from the table , so is there any way to call a function through that UIButton Variable.
// for reference
let x= cell.followButton;
Add a target to the button to call a method in your view controller. In this code, self is your view controller.
cell.followButton.addTarget(self, action: #selector(buttonPressed), forControlEvents: .TouchUpInside)
Then implement buttonPressed to handle segueing.
func buttonPressed() {
performSegueWithIdentifier("Identifier", sender: self)
}

Show viewController swift

I have a tableView and i made its cells custom and added a UIButton to the cell
i want when i press the button to popOver a new ViewController and in it the text in the cell i clicked the button in it. I don't want to use didSelectRowAtIndexPath func, i want to use the event of the button in each cell.
I tried to connect the button with the new viewController using popOver segue but the build fails because it is dynamic.
Please help and make the answer in swift.
Thanks in advance
You shouls set target to button inside the cell in cellForRowAtIndexPath function
cell.button.addTarget(self, action: "showPopupPage:", forControlEvents: UIControlEvents.TouchUpInside)
create a function
func showPopupPage(sender: UIButton) {
let view = button.superview!
let cell = view.superview as! custom cell
// you can get cell contents here
// call your popupview pass the value
}
Example=>
Swift - UIPopoverController in iOS 8

Resources