How to get button click from UIView to ViewController? - ios

I have created on custom View which contain one label and button
I have created outlet for both in UIView class
I have one UIViewController in which i have subview that custom view
now i want to change label name and want to do something on button click.
let commonView = UINib(nibName: "CommonView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CommonView
firstView.addSubview(commonView)
commonView.lblCommon.text = "We can change label in particular Screen"
I am able to change label text but how can i get button action event in UIViewcontroller?

Use protocol
In your customView declare a protocol
protocol CustomViewProtocol : NSObjectProtocol{
func buttonTapped()
}
Now create a variable in custom view
weak var delegate : CustomViewProtocol? = nil
In your viewController confirm to protocol
extension ViewController : CustomViewProtocol {
func buttonTapped() {
//do whatever u waana do here
}
}
set self as delegate in view controller
let commonView = UINib(nibName: "CommonView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CommonView
firstView.addSubview(commonView)
commonView.delegate = self
finally in IBAction of button in custom view trigger delegate
self.delegate?.buttonTapped()

In Swift 4:
If you want to catch the tap within your UIViewController do this in the viewDidLoad() after you instantiate your view from a nib (eg called aView):
let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
aView.addGestureRecognizer(tap)
Below viewDidLoad declare the following function and do whatever you want within its body:
#objc func tapAction() {
//Some action here
}
(A note: In Swift 4 the #objc annotation is obligatory, because it denotes that you want the function to be visible to the Obj-C runtime, which in UIKit handles (among other things) user interactions through its messaging system.)
Since this UIView instance belongs in the view hierarchy of this UIViewController it makes sense to handle the taps within this UIViewController. This gives you the ability to reuse this UIView within any other UIViewController and handle its tap logic from the respective UIViewController.
In case there is a particular reason you want the taps handled within the UIView, you can use the delegate pattern, a closure, or in a more advanced implementation a Reactive framework (ReactiveSwift or RxSwift/RxCocoa)

The way to do it with RxSwift/RxCocoa:
view.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext: { _ in
//react to taps
})
.disposed(by: stepBag)
more ways to manage Gestures, can be found at the RxSwift github page: https://github.com/RxSwiftCommunity/RxGesture

Related

get Nil when calling for IBOutlet properties in function

i’m working in swift and i’m trying to use the .frames to check if 2 objects of type CGRect intersect.
i have my View Controller Class and a CircleClass, the CircleClass creates a circle that has gesture recognition so i can drag the circles that i create where i want to, now i want to add the option that if at the end of the drag the subview intersects my trashimageView (image view that will always be in the low-right corner of the view for all devices it's like a trashcan) it can delete the circle or subView.
the problem is that when i try to call trashImageView.frame in a function “deleteSubView” that i’ve created in the View Controller i get nil and my app crashes.
But if the IBOutlet is in the VC and my function is defined in my VC, also i can call the trashImageView.frame (CGRect Value) in the viewDidLoad and there is fine, but not in my function, why do i get nil for this value??
class ViewController: UIViewController {
#IBOutlet var trashImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//here i can print the CGRect value just fine
print("my imageView init: \(trashImageView.frame)")
}
func deleteSubView(subView: UIView){
// Here i get nil from the trashImageView.frame
if (subView.frame.intersects(trashImageView.frame)) {
print("intersection")
subView.removeFromSuperview()
}
}
}
i've checked that the Nil value is from the 'trashImageView.frame' and that the connection with the storyboard is good.
i call the function ‘delete subView’ from another class but should that matter? i don’t understand what is the error here, why do i get nil? help please.
Since your UIViewController is declared and instantiated using storyboard my guess is that you are creating the view controller using it's no arg initializer, i.e.: let controller = MyController() if you must create an instance of the controller programmatically do so by obtaining a reference to the Storyboard that contains the controller, i.e like this:
NOTE: Here I'm using "MyController" as the name of the class and the identifier that has been set in the storyboard.
func createMyController() -> MyController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "MyController")
return controller as! MyController
}
I'd also add a guard for view load state in your deleteSubview(:subView) method, so something like this:
func deleteSubView(subView: UIView) {
guard isViewLoaded else { return }
// Here i get nil from the trashImageView.frame
if (subView.frame.intersects(trashImageView.frame)) {
print("intersection")
subView.removeFromSuperview()
}
}

How to use performSegue from UIView class in swift 3

I have an UIView class that contains a UILabel. I have added a UITapGestureRecognizer for the UILabel and want to do a performSegue to open a new UIViewController on tap of the UILabel.
The problem is that I can't use performSegue in UIView class.
Can anyone please help to use performSegue in UIView class?
I have added a tap gesture to my label as,
nameLabel.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
tap.isEnabled = true
nameLabel.addGestureRecognizer(tap)
Now in my tapFunction,
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
self.performSegue(withIdentifier: "memberSegue", sender: self)
}
But I get an error:
"Value of type UIViewController has no member performSegue"
as it is an UIView Class.
You should try and separate any functionality from your UIViews. A best practice is your UIViewControllers to be responsible for any functionality in your app and your UIViews to be used only for displaying elements. Thus I would suggest that you make your nameLabel a property in your UIView, so that you can access it from your UIViewController after instantiating the UIView. Then the code in your UIViewController should look like this:
let yourView = new YourView()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 1
tap.isEnabled = true
yourView.nameLabel.addGestureRecognizer(tap)
and you would have your tap() function in your UIViewController.
you can do it by protocol and delegate. Because actually you can not do it from UIView you should always do it from controller.
/// add this protocol in your project
protocol ViewProtocol {
func performSegueFromView()
}
class View : UIView {
/// add this line to your view
var delegate : ViewProtocol?
/// replace your tap function with this
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
self.delegate?.performSegueFromView()
}
}
class ViewController : UIViewController, ViewProtocol {
override func viewDidLoad() {
/// create view
let view = View()
/// assign view delegate
view.delegate = self
}
/// delegate function
func performSegueFromView() {
///peform segue from controller because you can not do this from uiview anyway
self.performSegue(withIdentifier: "memberSegue", sender: self)
}
}
Make delegate in UIView Class and used to where you are using UIView in YourController class
protocol UIViewDelegate {
func tapFunction() //this function will be use in controller class which uiview are using
}
var delegate: UIViewDelegate?
func tapFunction(sender:UITapGestureRecognizer) {
delegate?.tapFunction()
}
class YourController: UIViewController, UIViewDelegate {
let yourView = YourView()
yourView.delegate = self
func tapFunction() {
self.performSegue(withIdentifier: "memberSegue", sender: self)
}
}
I would suggest you to add action for that tap gesture instead of segue.
TapRecognizer.addTarget(self, action: #selector(YourFunc))
Samle Func:
func YourFunc(){
let storyBoardHome = UIStoryboard(name: "", bundle: nil)
let memberController = storyBoardHome.instantiateViewController(withIdentifier: "")
self.navigationController?.pushViewController(memberController, animated: true)
}
Refer This : https://stackoverflow.com/a/26366366/6818278
the error you wrote is wrong, UIViewController's do have that method. Your error must be "UIVIew has no member...", as for the solution you just need to get a reference to whatever UIViewController is using this specific view.
Then use that reference to call your perform method.
For example, if your custom View is being added programmatically, you can add an extra property to your class like
weak var myVC : UIViewController?
then after instantiating it just assign it to this property.
Then the perform segue becomes:
myVC?.performSegue....
Alternatively you can just add the tap gesture recognizer from the UIViewController that is using this view, then have the view show it from there.
Or you could just use:
UIApplication.shared.keyWindow?.rootViewController.performSegue...
But if you do, you should be careful as this one might have unintended consequences depending on what you are presenting and which is the current rootViewController.

Swift: Call Function in PageViewController from other Viewcontroller

I got an PageViewController which loads two "child "ViewControllers in order to let the user "swipe" through them. I don't want this swipe gesture , but instead I want to have a function inside my ViewController which allows me to use setViewControllers in the PageViewController.
I tried using protocols but even that didn't work out.
I would realy appreciate any help or suggestions on how I could accomplish that. Thanks!
To access setViewControllers from your child view controllers, you will need your child view controllers to be aware of their parent PageViewController. To do so, start by making a Protocol (I know you've said you've tried Protocols, but please please see my method through). This Protocol will ensure that every child view controller has a reference to the parent PageViewController.
protocol PageObservation: class {
func getParentPageViewController(parentRef: PageViewController)
}
Ensure that your child view controllers adhere to the PageObservation Protocol.
class Child1ViewController: UIViewController, PageObservation {
var parentPageViewController: PageViewController!
func getParentPageViewController(parentRef: PageViewController) {
parentPageViewController = parentRef
}
}
class Child2ViewController: UIViewController, PageObservation {
var parentPageViewController: PageViewController!
func getParentPageViewController(parentRef: PageViewController) {
parentPageViewController = parentRef
}
}
In your PageViewController, as you create each child view controller, cast them to the PageObservation type and pass a reference of the parent PageViewController. I use an array called orderViewControllers to create my pages. My UIPageViewControllerDataSource delegate methods uses it to know which pages to load but that is irrelevant to this example, I just thought I'd let you know in case you have a different way of creating your pages.
class PageViewController: UIPageViewController {
var orderedViewControllers: [UIViewController] = []
//creating child 1
//i am using storyboard to create the child view controllers, I have given them the identifiers Child1ViewController and Child2ViewController respectively
let child1ViewController = UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "Child1ViewController")
let child1WithParent = child1ViewController as! PageObservation
child1WithParent.getParentPageViewController(parentRef: self)
orderedViewControllers.append(child1ViewController)
//creating child 2
let child2ViewController = UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "Child2ViewController")
let child2WithParent = child2ViewController as! PageObservation
child2WithParent.getParentPageViewController(parentRef: self)
orderedViewControllers.append(child2ViewController)
}
Now inside your child view controllers, you have access to setViewControllers. For example, if I want to call setViewControllers in the child1ViewController, I have created a func called accessSetViewControllers() where I access the setViewControllers:
class Child1ViewController: UIViewController, PageObservation {
var parentPageViewController: PageViewController!
func getParentPageViewController(parentRef: PageViewController) {
parentPageViewController = parentRef
}
func accessSetViewControllers() {
parentPageViewController.setViewControllers( //do what you need )
}
}
On a side note, despite what other answers above have said, you can set dataSource to whatever you like. I sometimes set dataSource to nil to prevent the user from swiping away from a screen before doing something and then add the dataSource back to allow them to continue swiping.
Don't set dataSource. When it's nil, then gestures won't work.
https://developer.apple.com/reference/uikit/uipageviewcontroller
When defining a page view controller interface, you can provide the content view controllers one at a time (or two at a time, depending upon the spine position and double-sided state) or as-needed using a data source. When providing content view controllers one at a time, you use the setViewControllers(_:direction:animated:completion:) method to set the current content view controllers. To support gesture-based navigation, you must provide your view controllers using a data source object.
Simplistic approach... remove the inbuilt gesture recogniser in viewDidLoad of pageViewController:
for view in self.pageViewController!.view.subviews {
if let subView = view as? UIScrollView {
subView.scrollEnabled = false
}
}
Then add your own gesture below it. i just happened to be working with double tap at the moment but you could make it swipe left, swipe right easy enough:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTap.numberOfTapsRequired = 2
doubleTap.delaysTouchesBegan = true
self.addGestureRecognizer(doubleTap)
and the gesture function with your code:
func didDoubleTap(gesture: UITapGestureRecognizer) {
//... stuff
}

Sending information between ViewControllers in swift

I have two classes:
class ExplorerViewController: UIViewController {
lazy var studyButton: ExploreButton = {
let button = ExploreButton()
button.setTitle("Study", forState: .Normal)
return button
}()
}
and
class ViewController: UIViewController, UISearchBarDelegate, LocateOnTheMap, GMSMapViewDelegate {
}
I'm trying to make it so that when I click the studyButton, it sends the button title to ViewController and goes to that view.
I'm not using storyboards and am having trouble with segues since every tutorial seems to give different examples that are specific to the things they've been working with and 95% of them seem to be operating with storyboard. Can someone give me a general way of how to do this?
How do I give the starting view controller an identifier because it isn't instantiated like the other controllers that I 'move' to after. How can I move from ViewController to ExplorerViewController and then move back to that same ViewController (with all changes intact).
Create an initializer for your ViewController that receives the "title" variable:
class ViewController: UIViewController, UISearchBarDelegate, LocateOnTheMap, GMSMapViewDelegate {
var btnTitle: String?
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?, btnTitle:String?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.btnTitle = btnTitle
}
}
When creating the ViewController object use this initializer.
var viewController = ViewController(nibName: "ViewController", bundle: nil, btnTitle: title
You can initialize UIViewController that you want navigate to, assign data to properties in that controller and call this method:
presentViewController(viewController, animated: true, completion: nil)
For example:
let destinationViewController = ViewController()
destinationViewController.frame = self.view.frame
destinationViewController.buttonTitle = "title"
self.presentViewController(destinationViewController, animated: true, completion: nil)
Although I would suggest you to get familiar with Storyboards and perform navigation with Segues.
Make sure of two things:-
1.) You have given your viewController an StoryBoard ID lets say "viewControllerVC_ID" in it's Identity inspector
2.) You have NavigationController Embed in to your Initial entry point View Controller
In ViewController declare a variable
var btnLabelTxt : String!
Create an #IBAction of that button in ExplorerViewController :-
#IBAction func exploreBtnAction(sender : UIButton!){
let vcScene = self.navigationController?.storyboard?.instantiateViewControllerWithIdentifier("viewControllerVC_ID") as! ViewController
vcScene.btnLabelTxt = "Study"
//or you can just access the button itself in the viewController and set the title
//By vcScene.yourBtn.setTitle("Study", forState: .Normal)
self.navigationController?.pushViewController(vcScene, animated: true)
}
please see this question How to push viewcontroller ( view controller )? for how to switch between views.
to pass data once you have reference to the new view, you can assign the data to a property of that view.

How to present another view controller by tapping on a label inside a tableview cell

I want to create a segue to from an object (label) inside a tableview cell to another view controller.
example: imagine the instagram feed tableview - tapping the number of likes under an image will push a second view with the list of people who liked an image.
How can i do that?
I know it's possible when tapping a cell but i need a solution for when tapping a label inside the cell...
thanks!
You have to make the following steps :
Define userInteractionEnabled = true for the label using Interface Builder or in code, as you wish, (VERY IMPORTANT!!) without this the tap is not enabled.
Define an action to the Tap Gesture Recognizer to handle the tap inside the label.
To present the another ViewController you can do one of the following two things :
Make a modal segue from the Tap Gesture Recognizer to the another ViewController using the Interface Builder or make an action for the Tap Gesture Recognizer and then present the another ViewController manually using the presentViewController function.
I thought the first is more easy to present the another ViewController, Is up to you.
I hope this help you.
You can simply use a button and set its title instead of using a label. If you have to use a UILabel for some reason, then you can do so by setting its userInteractionEnabled property to true and then adding a UITapGestureRecognizer to it.
You can use UITapGestureRecognizer.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblTxt: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleTap:"))
recognizer.numberOfTapsRequired = 1;
lblTxt.addGestureRecognizer(recognizer)
lblTxt.userInteractionEnabled = true;
}
func handleTap(recognizer: UITapGestureRecognizer){
println("tapped!")
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("secondView") as SecondViewController
self.presentViewController(secondViewController, animated:true, completion:nil)
}

Resources