How to add button target function in IBAction method - swift

I am using SWRevealViewController for side menu. while i click on menuBtn, im performing right toggle operation. I want the same when i swipe right Can any one Please Help me with it.
class Invoice: UIViewController {
#IBOutlet weak var menuBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let revealViewController = self.revealViewController()
menuBtn.addTarget(revealViewController, action: #selector(SWRevealViewController.rightRevealToggle(_:)), for: .touchUpInside)
}
#IBAction func swipeGesture(_ sender: UISwipeGestureRecognizer) {
print("Swiped")
//I want same action as i clicked on mentBtn
}
}

Something like this should work:
#IBAction func swipeGesture(_ sender: UISwipeGestureRecognizer) {
self.revealViewController().rightRevealToggle(self.menuBtn)
}

Related

How to disable a button within a anyObject Button in swift

Ok so I want to be able to disable the button within itself. So when I press the button it will disable itself.
import UIKit
class ViewController: UIViewController {
#IBOutlet var buttons: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func but(_ sender: AnyObject) {
//this code doesn't seem to work at all, and I don't know why
(sender as AnyObject).isEnabled = false
}
}
It gives me an error message when ever I try to do this "Cannot assign to immutable expression of type '#lvalue Bool?'"
So is there a different way I can disable this button
Presumably, you've hooked up this IBAction in Interface Builder to a UIButton. So, you can change your function signature to:
#IBAction func but(_ sender: UIButton) {
sender.isEnabled = false
}
AnyObject doesn't have a property called isEnabled, which is why your previous code failed.
within the button I use this code
#IBAction func but(_ sender: AnyObject) {
var disableMyButton = sender as? UIButton
disableMyButton?.isEnabled = false
}

Change textcolor with click on different button

I have two IBAction and want to change of the text of two buttons who refer to these IBActions.
I can change with sender. the color of the own button. But not with the other button. How can I solve this? My Code doesnt work. It only changes the own color.
#IBAction func arminiaBielefeld(_ sender: UIButton) {
team = "arminia"
Bundesliga1.bildlink = "bundesliga1/arminia"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
sender.setTitleColor(.white, for: [])
augsburg.setTitleColor(.blue, for: [])
}
#IBAction func augsburg(_ sender: UIButton) {
team = "augsburg"
Bundesliga1.bildlink = "bundesliga1/augsburg"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
sender.setTitleColor(.white, for: [])
arminiaBielefeld.setTitleColor(.blue, for: [])
}
Screenshot added.
Try using the outlet name of the button instead of sender. Also do not have the name of the action identical to the name of the outlet.
Add the following two outlets to your view controller and connect them in storyboard:
#IBOutlet weak var arminiaBielefeld: UIButton!
#IBOutlet weak var augsburg: UIButton!
The following works:
#IBAction func arminiaBielefeldPressed(_ sender: Any) {
team = "arminia"
Bundesliga1.bildlink = "bundesliga1/arminia"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
arminiaBielefeld.setTitleColor(.white, for: [])
augsburg.setTitleColor(.blue, for: [])
}
#IBAction func augsburgPressed(_ sender: Any) {
team = "augsburg"
Bundesliga1.bildlink = "bundesliga1/augsburg"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
augsburg.setTitleColor(.white, for: [])
arminiaBielefeld.setTitleColor(.blue, for: [])
}

how to add pan gesture on long pressing a button [duplicate]

I want to trigger two action on button click and button long click. I have add a UIbutton in my interface builder. How can i trigger two action using IBAction can somebody tell me how to archive this ?
this is the code i have used for a button click
#IBAction func buttonPressed (sender: UIButton) {
....
}
can i use this method or do i have to use another method for long click ?
If you want to perform any action with single tap you and long press the you can add gestures into button this way:
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}
#objc func tap() {
print("Tap happend")
}
#objc func long() {
print("Long press")
}
This way you can add multiple method for single button and you just need Outlet for that button for that..
#IBOutlet weak var countButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
addLongPressGesture()
}
#IBAction func countAction(_ sender: UIButton) {
print("Single Tap")
}
#objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 1.5
self.countButton.addGestureRecognizer(longPress)
}
Why not create a custom UIButton class, create a protocol and let the button send back the info to delegte. Something like this:
//create your button using a factory (it'll be easier of course)
//For example you could have a variable in the custom class to have a unique identifier, or just use the tag property)
func createButtonWithInfo(buttonInfo: [String: Any]) -> CustomUIButton {
let button = UIButton(type: .custom)
button.tapDelegate = self
/*
Add gesture recognizers to the button as well as any other info in the buttonInfo
*/
return button
}
func buttonDelegateReceivedTapGestureRecognizerFrom(button: CustomUIButton){
//Whatever you want to do
}

textfield not empty show save button in Swift

My save button is hidden with the function saveBtnHidden() in the code below. However, the save button won't reappear when text is typed into the text field. I have tried multiple solutions similar to this. Every time that I type into the text field, the save button just won't show up.
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
}
func saveBtnHidden() {
if (nicknameField.text?.isEmpty ?? true) {
// is empty
saveBtn.isHidden = true
} else {
saveBtn.isHidden = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
performSegue(withIdentifier: "nextPage", sender: nil)
}
}
You are getting this error because your function saveBtnHidden() is only called once in viewDidLoad(). It does not get called again when the text in your text field changes. To detect when text changes, you will need to add a target to your text field that calls a function when it changes (.editingChanged) like this:
nicknameField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Then in the textFieldDidChange call your saveBtnHidden() function:
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
Code adapted from: How do I check when a UITextField changes?
Use delegate to be notify of any change. Delegates is a key part of iOS development and Apple's framework.
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
nicknameField.delegate = self
}
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
// More of your source code below...

perform segue with the same button to two different viewControllers based on information in uitextfield

I am trying to make my view controller segue to two different view controllers with the same button. But i want the segue's to be done based on information that is in an uitextfield. e.g = if the textfield contains the right information then i will need the segue to perform to segue to viewControllerA if the information does not match my array of strings, ViewController will segue to viewControllerB.
Sample code:
#IBOutlet var pcTextField: UITextField!
#IBOutlet var odButton: UIButton!
var activePcText = ["over", "left", "weak", "never"]
override func viewDidLoad() {
super.viewDidLoad()
pcTextField.delegate = self
let tapRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard")
view.addGestureRecognizer(tapRecognizer)
odButton.hidden = true
self.pcTextField.addTarget(self, action: "pcEmpty", forControlEvents: UIControlEvents.EditingChanged)
}
#IBAction func odButton(sender: AnyObject) { }
I tried to implement an if statement as..
#IBAction func odButton(sender: AnyObject) {
if pcTextField.text!.containsString(activePcText) { }
}
I just get an error saying:
Cannot convert value of type '[String]' to expected argument type 'String'
#IBOutlet weak var pcTextField: UITextField!
let activePcText = ["over", "left", "weak", "never"]
#IBAction func buttonPressed(sender: UIButton) {
let text = pcTextField.text?.lowercaseString ?? ""
let destination = activePcText.map { $0.lowercaseString }.contains(text) ? "viewControllerA" : "viewControllerB"
performSegueWithIdentifier(destination, sender: self)
}
This makes it case insensitive.
This is an example of a prepareForSegue method that would accomplish what it sounds like you want to do
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueIdentifier" {
if myTextField.text = "words inside text field" {
let controller = segue.destinationViewController as! MyViewController
} else if myTextField.text = "different words in text field" {
let controller = segue.destinationViewController as! MyOtherViewController
}
}
}
So depending on the text inside of myTextField, the segue will be performed to either MyViewController or MyOtherViewController
And then in the #IBAction of whichever button you want to trigger the segue you would add performSegueWithIdentifier("segueIdentifier")