Make a button clickable inside a viewCell, UIViewCollection - swift

I have created a .xib view cell view and it contains a button, label, and some text inside, how do I make the button clickable and get the details of the view ie, the label, and text?

A way to do this is by adding a tag and target to the button.
cell.moreBtn.tag = indexPath.row
cell.moreBtn.addTarget(self, action: #selector(showClickedBtn(sender:)),
for: .touchUpInside)
After that, create a function that will receive the action.
#objc func showClickedBtn(sender: UIButton) {
print("Button \(sender.tag) Clicked")
}

Related

Nav button needs to go form the last view to the first view

Hello, I have provided the link above to show a visual view of what I am trying to accomplish. So each button is a segue to the next view except the last button is not which is intended. The last two views with the back buttons at the top that are automatically included because of the nav controller I want to have those back buttons go to the first view controller. How can I do this either with xcode and swift?
If you want to remove all the navigation controller then use this code
Put this code in the button selector method
self.navigationController!.viewControllers.removeAll()
OR
navigationController?.popToRootViewController(animated: true)
OR
If you don’t have a Navigation Controller and use dismissViewController method, you still can use:
view.window?.rootViewController?.dismiss(animated: true, completion: nil)
Now comes the back button code:
override func viewDidLoad() {
super.viewDidLoad()
var backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Any image can be used. download back button image from any site or whatever you wanna use.
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}
func backAction() -> Void {
//here put code from above whichever you wanna use for example I'm using one
self.navigationController?.popToRootViewController(animated: true)
}

How to write function which change button color

I have a small problem. I created a short quiz app, and when we choose the answer the answer is green. I need a function that will change all buttons to the basic color, blue
First you should have array with references for your buttons. If you use interface builder, you can create outlet collection
#IBOutlet var buttons: [UIButton]!
Then, when button is pressed, change color for each button depending on if element is equal to sender of action for button pressed event
#IBAction func buttonPressed(_ sender: UIButton) {
for button in buttons {
button.backgroundColor = button == sender ? .green : .blue
}
}

How can I have a button and scroll view in the same spot?

To build context, my app has a stack of cards (similar to Tinder) that flip and each could contain a lot of text on one side that require a UITextView for scrolling. It's basically a flashcard app, so once the user is done looking at the card, they swipe it away to view the next one.
I'm trying to make it so the user can tap anywhere on the card to flip it, so I added a clear button that fills up the card. My problem is that when the button is at the front, it blocks the text view from being scrollable, and when the button is at the back, the text view blocks it from being pressed.
Is there a way around this somehow to allow for both functionalities?
You can create a UITapGestureRecognizer, like so:
let gesture = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
addGestureRecognizer(gesture)
And the function to trigger:
#objc func tap(gesture: UITapGestureRecognizer) {
print("Tap!")
}
Maybe you can add a UITapGestureRecognizer to the text view itself. Something like:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(_:)))
textView.addGestureRecognizer(tapGesture)
#objc func didTap(_ gesture: UITapGestureRecognizer) {
// Handle the tap
}
did you try a tableView or a collectionView, you can customize your cell to look like a card then when a user clicks the cell it will flip or present a popup view, thats easier.

set image for UIbutton selected state not working in swift 4

I have swift4 in my project and set an image for button state selected. But, when the button is in the selected state the image is not getting changed. I have changed the image in storyboard as well as code but nothing works.
ChkBoxBtn.setImage(UIImage(named: "unCheck"), for: [])
ChkBoxBtn.setImage(UIImage(named: "alertCheck"), for: .selected)
When a user taps a button the associated action will be called.
Within that action or method you have to change the .isSelected property of the concerned UIButton and set it to true
yourButton.isSelected = true
This will then reflect the image you have set in storyboard for the selected state of the UIButton
Create an action outlet to view controller, then add the below code. This helps you to switch between selected and deselected states easily.
#IBAction func ChkBoxBtnTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
}

swift swreveal - showing toggling to side menu using button or tap gesture

I'm trying to get my side menu (swreveal) to show when clicking a UIImageView with a tap gesture or button. I've got the image view and tap all set up and the tap gesture linked to an action. Getting this functionality is easy when using a bar button item but I can't seem to get it to function when using a plain old button or image view.
Here's how I do it for a bar button item:
barButtonItem!.target = self.revealViewController()
barButtonItem!.action = Selector("revealToggle:")
This obviously doesn't work for buttons or ImageViews because they don't have a member of target or action. Would really appreciate some assistance on this one!
To add target to UIButton you can use this:
...
let button = UIButton()
button.addTarget(self, action: "someAction:", forControlEvents: .TouchUpInside)
...
func someAction(sender: UIButton!) {
...
}
For adding action to press on UIImageView you can use UITapGestureRecognizer like this:
...
let tap = UITapGestureRecognizer(target: self, action: Selector("someAction"))
tap.numberOfTapsRequired = 1
image.userInteractionEnabled = true
image.addGestureRecognizer(tap)
...
func someAction() {
...
}