How can I call an action through UIButton variable - ios

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)
}

Related

How to segue to a view controller by touching a UIView?

How can I segue to a view controller by touching a view embedded inside my view controller's view? It's easy to do this with a UIButton but not a UIView. Can anyone help please?
Add a UITapGestureRecognizer to the subview then performSegue in the selector method.
subview.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleViewTap)))
Then:
#objc func handleViewTap() {
performSegue(withIdentifier: "identifier", sender: self)
}

Is is possible to click an imageView and open a new View Controller?

I'm new to IOS and learning the IDE at the moment. I was wondering if its possible to link an imageView, when clicked, to a View Controller
Sure. First, in viewDidLoad of your UIViewController make it tappable:
imageView.isUserInteractionEnabled = true
Then assign the gesture recognizer:
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.imageTap)))
Once the action is triggered, perform a transition to a new VC:
// Func in your UIViewController
#objc func imageTap() {
// present modally
self.present(YourNewViewController())
// or push to the navigation stack
self.navigationController?.push(YourNewViewController())
// or perform segue if you use storyboards
self.preformSegue(...)
}
Yes. It is possible. You'll need to add a tap gesture recogniser to image view and in the function perform segue.
SWIFT 4
let singleTap = UITapGestureRecognizer(target: self,action:Selector(“imageTapped”))
yourImageView.isUserInteractionEnabled = true
yourImageView.addGestureRecognizer(singleTap)
Function to handle Tap:
#objc func imageTapped() {
performSegue(withIdentifier: "yourNextScreen", sender: "")
}
Yes. You can bind a tap gesture recognizer to your imageview. You can follow this link here on a previous answer on how to do it programmatically
You could do it completely in the interface builder by putting a UIButton, with a transparent background and no text, over the ImageView and then control drag the button to the viewcontroller you want to trigger a segue on touch.
Of course it is. You should add a UITapGestureRecognizer to the UIImageView and add a selector to trigger the pushing a new viewcontroller method.
For example;
#IBOutlet weak var imageView: UIImageView! {
didSet {
let imageTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
imageView.addGestureRecognizer(imageTapGestureRecognizer)
imageView.isUserInteractionEnabled = true
}
}
func imageTapped() {
//navigate to another view controller
}
add gesture and if you want to animate it then see this pod

Swift 4 - How to activate segue programmatically from button nested in UICollectionViewCell

I am trying to activate a segue between a UICollectionView, subview of a UIViewController, and a separate ViewController (called RegisterController). The CollectionView is all set up programmatically, and there is a button on the last cell. However, I can't do the typical performSegue(_:withIdentfier) because the button is in the CollectionView. So, I have set up a method in the ViewController holding the CollectionView to performSegue(_:withIdentifier). I set up an instance in the CollectionViewController to call the method:
lazy var registerButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Don't have an account?", for: .normal)
btn.setTitleColor(lighterOrange, for: .normal)
btn.addTarget(self, action: #selector(registerScreenAppear), for: .touchUpInside)
return btn
}()
#objc func registerScreenAppear(){
let vc: ViewController = ViewController()
_ = vc.toRegister()
}
The method in the ViewController is
func toRegister(){
performSegue(withIdentifier: "toRegister", sender: self)
}
I am getting the call stack:
NSInvalidArgumentException', reason: 'Receiver (<audible.ViewController: 0x7fe780724770>) has no segue with identifier 'toRegister''
I know I'm probably doing something completely wrong, but I've searched and searched and can't find a solution. Any help is appreciated, thanks
(Yes, I have configured the segue and gave it an identifier in the storyboard, but the reason I have to do this through code is because the CollectionView and the button are created programmatically, so I can't use the Interface Builder)
call performSegue in registerScreenAppear method or call self.toRegister() like
#objc func registerScreenAppear(){
self.toRegister()
}
OR
#objc func registerScreenAppear(){
performSegue(withIdentifier: "toRegister", sender: self)
}
Note: use later one as it will remove the extra function call i.e. toRegister().

UIButton cannot clicked after adding to another uiview

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.

how perform IBAction declared in separate cell class from collection view controller

I create UIButton in the collection view cell, and set the appearance state to hidden.
Also I declared #IBAction to delete items from collection view
class MyCell: UICollectionViewCell {
#IBAction func deleteButtonTapped(sender: AnyObject) {
// deletion code
}
in the collection view, I've navigation bar button, which cause that the delete button appears.
How to perform the the deleteButtonTapped action that was previously declared in cell class, after I tap the delete button.
You should be able to connect your delete button to the delete action in your collection view cell subclass from the storyboard. You can also do it programmatically by first creating an outlet in your subclass and associating your delete action to your button.
#IBOutlet weak var deleteButton: UIButton! {
didSet {
deleteButton.addTarget(self, action: #selector(deleteButtonTapped(_:)), forControlEvents: .TouchUpInside)
}
}

Resources