First UIButton fires another UIbutton event of same view - ios

i have create two UIButton in swift and added action programmatically. When i try to click on the first button both button action are fired. But when i click on the second button none of the events are firing.
var btnSort = UIButton(frame: CGRectMake(2, 74, 140, 26))
btnSort.setTitle("SORT", forState: UIControlState.Normal)
btnSort.backgroundColor = UIColor.grayColor()
btnSort.tag=10
btnSort.addTarget(self, action: Selector("showSortTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btnSort)
var btnFilter = UIButton(frame: CGRectMake(140+16+2, 74, 140, 26))
btnFilter.backgroundColor = UIColor.redColor()
btnFilter.tag=11
btnFilter.setTitle("FILTER", forState: UIControlState.Normal)
btnSort.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btnFilter)
func showSortTbl(sender: UIButton){
var btnSendTag :UIButton = sender
if(btnSendTag.tag == 10){
println("show sort")
}
}
func showFilterTbl(sender: UIButton){
var btnSendTag :UIButton = sender
if(btnSendTag.tag == 11){
println("show filter")
}
}

Change this line
btnSort.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
to
btnFilter.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)

Related

Swift add show action to button programmatically

how can I add action to button programmatically. I need to add show action to buttons in mapView. thanks
let button = UIButton(type: UIButtonType.Custom) as UIButton
You can go for below code
`
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle("Click Me", for: .normal)
btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
btn.tag = 1
self.view.addSubview(btn)
for action
#objc func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
dismiss(animated: true, completion: nil)
}
}
let button = UIButton(type: UIButtonType.Custom) as UIButton
button.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)
//then make a action method :
func action(sender:UIButton!) {
print("Button Clicked")
}
You need to add a Target to the button like Muhammad suggest
button.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)
But also you need a method for that action
func action(sender: UIButton) {
// Do whatever you need when the button is pressed
}
For swift 5 users can do by this simple way
cell.followButton.tag = 10
cell.followButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
#objc func buttonAction(sender: UIButton!) {
let id = sender.tag
print(id)
}
in addition to the above, the new ios14 introduce
if #available(iOS 14.0, *) {
button.addAction(UIAction(title: "Click Me", handler: { _ in
print("Hi")
}), for: .touchUpInside)
} else {
// Fallback on earlier versions
}
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton()
btn.frame = CGRectMake(10, 10, 50, 50)
btn.setTitle("btn", forState: .Normal)
btn.setTitleColor(UIColor.redColor(), forState: .Normal)
btn.backgroundColor = UIColor.greenColor()
btn.tag = 1
btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btn)
}
For Swift 4 use following:
button.addTarget(self, action: #selector(AwesomeController.coolFunc(_:)), for: .touchUpInside)
//later in your AswesomeController
#IBAction func coolFunc(_ sender:UIButton!) {
// do cool stuff here
}

Add a badge to UIButtonitem with MIBadgeButton-Swift

I'm trying to add a badge to my UIBarButtonItem and for that I have found a this github :
MIBadgeButton-Swift
but I don't know how to use it.
This is my code which makes my custom UIBarButtonItem:
let shopingCartBTN = UIButton(type: UIButtonType.Custom)
shopingCartBTN.setImage(UIImage(named: "shopingCarBarIcon"), forState: UIControlState.Normal)
shopingCartBTN.imageView?.image = UIImage(named: "shopingCarBarIcon")
shopingCartBTN.frame = CGRectMake(0, 0, 60, 30)//Just increase the width of button
shopingCartBTN.setTitle("5", forState: .Normal)
shopingCartBTN.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside)
let customBarItem = UIBarButtonItem(customView: shopingCartBTN)
self.navigationItem.leftBarButtonItem = customBarItem;
How could I use the MIBadgeButton-Swift to make a badge for my UIBarButtonItem?
Here is two example with custom view and from storyboard
From storyboard by setting custom class :
CODE
#IBOutlet var btnRightBadge: MIBadgeButton!
override func viewDidLoad() {
super.viewDidLoad()
//Custom
let badgeButton : MIBadgeButton = MIBadgeButton(frame: CGRectMake(0, 0, 40, 40))
badgeButton.setTitle("T1", forState: UIControlState.Normal)
badgeButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
badgeButton.badgeString = "1";
let barButton : UIBarButtonItem = UIBarButtonItem(customView: badgeButton)
self.navigationItem.leftBarButtonItem = barButton
//From Storyboard
btnRightBadge.badgeString = "5"
}
OUTPUT
You can also use ENMBadgedBarButtonItem-Swift
Try this once.
//Property
var cartBarbuttonItem:MIBadgeButton?
self.cartBarbuttonItem = MIBadgeButton(frame: CGRectMake(40, 5, 40, 44))
self.cartBarbuttonItem?.initWithFrame(frame: CGRectMake(40, 5, 40, 44), withBadgeString: "0", withBadgeInsets: UIEdgeInsetsMake(15, 2, 0, 15))
self.cartBarbuttonItem?.setImage(UIImage(named: "test"), forState: .Normal)
self.cartBarbuttonItem?.setImage(UIImage(named: "test"), forState: .Selected)
self.cartBarbuttonItem?.addTarget(self, action: Selector("loadCart"), forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.setLeftBarButtonItem = self.cartBarbuttonItem

How to create a custom UIButton with same Buttonsystemitem click effect

I'm creating a custom button item for my navigation bar, and I want it to have the same effect as the button system item ( Slow fade out )
Here is my code :
var button_user : UIImage = UIImage(named:"user.png")!
let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
button.setImage(UIImage(named: "user.png"), forState: UIControlState.Normal)
button.setImage(UIImage(named: "user_clicked.png"), forState: UIControlState.Highlighted)
button.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 28, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
You should change UIButtonType.Custom to UIButtonType.System for that:
let button = UIButton(type: UIButtonType.System)
button.setImage(UIImage(named: "user.png"), forState: UIControlState.Normal)
button.setImage(UIImage(named: "user_clicked.png"), forState: UIControlState.Highlighted)
button.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 28, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
Edit
Updated for Swift 2.0
button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonClicked(sender:UIButton)
{
dispatch_async(dispatch_get_main_queue(), {
if isHighLighted == false{
sender.highlighted = true;
isHighLighted = true
}else{
sender.highlighted = false;
isHighLighted = false
}
});
}
OR
button.setShowsTouchWhenHighlighted(true)
Edit: You should use UIBarButtonItem instead of UIButton in the navigation bar. That will do.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "backbutton"), style: UIBarButtonItemStyle.Plain, target: self, action: "buttonClicked:")
And also add the method
func buttonClicked(sender: AnyObject){
//Enter your code here
}
add more "images" for the UIControlState accordingly

How to change element title inside UITableViewCell?

I have a button on custom tableviewcell. I've been able add action to this button by add target like below code:
var playButton = cell.contentView.viewWithTag(5) as? UIButton
var sender : UIButton = UIButton()
playButton?.addTarget(self, action: "playPost:", forControlEvents: UIControlEvents.TouchUpInside)
playButton?.layer.setValue(object["previewUrl"] as? String, forKey: "songUrl")
playButton?.layer.setValue(indexPath.row, forKey: "index")
But, how can I change it's title whenever I clicked this button?
If your function is playPost(), you could change the value passed in (which is the sender of the function, by default) like this:
func playPost(sender: UIButton) {
sender.setTitle("New title", forState: UIControlState.Normal);
}
You can Change UIButton title by using setTitle Method
Example
var button = UIButton(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
button.backgroundColor = UIColor.blackColor()
button.setTitle("Start", forState: UIControlState.Normal)
button.addTarget(self, action: "pressButton:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
func pressButton(sender:UIButton)
{
if sender.titleLabel?.text == "Start"
{
sender.setTitle("Stop", forState: UIControlState.Normal)
}
else
{
sender.setTitle("Start", forState: UIControlState.Normal)
}
}

Set button type

I am creating a button programatically and I would like to know how to set that button's UIButtonType.
This is how I am creating my button:
let headerPlusButon:UIButton = UIButton(frame: CGRectMake(5, tableView.frame.width - 30, 20, 20))
headerPlusButon.addTarget(self, action: "paymentAddButtonTapped", forControlEvents:.TouchUpInside)
There is no "setButtonTypeFor..." method like how you would change the title's text and obviously this doesn't work:
headerPlusButon.buttonType = UIButtonType.ContactAdd
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender:UIButton!)
{
println("Button tapped")
}
from swift 2 replace UIButton declaration with below line.
let button = UIButton(type: .System) as UIButton
Here is my code
let tutorialButton = UIButton.init(type: .infoDark)
tutorialButton.frame = CGRect(x: self.view.frame.size.width - 20, y: 42, width: 20, height: 20)
tutorialButton.addTarget(self, action: #selector(self.showTutorial), for: .touchUpInside)
view.addSubview(tutorialButton)
I hope it can help you!

Resources