In my viewController there are one button and textField. When use click on textField it fires didBeginEdit event and i can call a toolbar.
This is my view :
MyView
When user click on textField it seems like below :
textFieldClicked
And My Swift code is below :
Class ViewController: UIViewController {
#IBAction func btn(sender: AnyObject) {
// i want to call ToolBar here ...
}
#IBOutlet weak var textFieldOutlet: UITextField!
#IBAction func txtFieldBeginEdit(sender: AnyObject) {
raiseToolBar(textFieldOutlet)
}
var datePicker : UIDatePicker!
let ToolBar = UIToolbar()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func raiseToolBar(textField : UITextField)
{
self.datePicker = UIDatePicker(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.height , UIScreen.mainScreen().bounds.height / 3))
self.datePicker.backgroundColor = UIColor.whiteColor()
self.datePicker.datePickerMode = .Date
textField.inputView = self.datePicker
ToolBar.barStyle = .Default
ToolBar.translucent = true
ToolBar.tintColor = UIColor(red : 92/255, green : 216/255 ,blue : 255/255, alpha: 1 )
ToolBar.sizeToFit()
let done = UIBarButtonItem(title: "Tamam" , style: .Plain , target: self, action: #selector(ViewController.doneClick))
let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target : nil , action: nil)
let cancel = UIBarButtonItem(title: "İptal" , style: .Plain , target: self, action: #selector(ViewController.cancelClick))
ToolBar.setItems([done , space , cancel], animated: true)
ToolBar.userInteractionEnabled = true
textField.inputAccessoryView = ToolBar
}
func doneClick() {
}
func cancelClick() {
}
}
At this point there is no problem. Bu i want to call this UIToolBar by clic on the button(named btn). Under btnClick event i tried to send empty textField to raiseToolBar method but it also don't work. How can i edit my code to call ToolBar by buttonClick
**************** edit *****************
textFieldOutlet.becomeFirstResponder()
raiseToolBar(textFieldOutlet)
i solve like above. is it a clear way ?
You can assign first responder to your text field like this inside your btn action:
textFieldOutlet.isFirstResponder = true
Related
I have such screen:
When you click on a textfield, the top of the keyboard should have "two arrows" on the left and the button "Done" on the right.
Like this:
I must say right away that I used Iqkeyboardmanager and because of this library there were problems therefore I had to remove it from the project. So help do it manually.
In the project, I already implemented the Done button, my code:
extension UITextField {
func addInputAccessoryView(title: String, target: Any, selector: Selector) {
let toolBar = UIToolbar(frame: CGRect(x: 0.0,
y: 0.0,
width: UIScreen.main.bounds.size.width,
height: 44.0))
let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let barButton = UIBarButtonItem(title: title, style: .plain, target: target, action: selector)
toolBar.setItems([flexible, barButton], animated: false)
self.inputAccessoryView = toolBar
}
}
final class ProfileSettingsViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate {
#objc func tapDone() {
self.view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.nameTF.addInputAccessoryView(title: "Done", target: self, selector: #selector(tapDone))
self.companyTF.addInputAccessoryView(title: "Done", target: self, selector: #selector(tapDone))
self.nameTF.delegate = self
self.companyTF.delegate = self
.....
The rest of the code
}
}
What i have at the moment:
I need to add two black arrows on the left and make the Done buttons black. Help me please.
If you think my code is bad, you can provide your correct example. I am new in ios development.
1.Create a custom delegate which will notify the ViewController when some button is pressed inside the Input Accessory View.
protocol MyCustomTextFieldDelegate: class {
func doneButtonPressed()
func arrowDownPressed()
func arrowUpPressed()
}
2.Create a custom subclass of the the UITextField and make a function that will create the Input Accessory View. Define the newly created MyCustomTextFieldDelegate as a weak class property. (Note: we are making this subclass, as we are unable to add the MyCustomTextFieldDelegate as a property to the UITextField directly)
class MyCustomTextField: UITextField {
weak var customTextFieldDelegate: MyCustomTextFieldDelegate?
func enableInputAccessoryView() {
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self,
action: #selector(textFieldDonePressed))
doneButton.tintColor = .black
let arrowDown = UIBarButtonItem(image: UIImage(named: "arrow-right"), style: .done, target: self, action: #selector(arrowUpPressed))
arrowDown.tintColor = .black
let arrowUp = UIBarButtonItem(image: UIImage(named: "arrow-right"), style: .done, target: self, action: #selector(arrowDownPressed))
arrowUp.tintColor = .black
toolBar.setItems([arrowUp, arrowDown, space, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
inputAccessoryView = toolBar
}
#objc private func textFieldDonePressed() {
endEditing(true)
customTextFieldDelegate?.doneButtonPressed()
}
#objc private func arrowDownPressed() {
customTextFieldDelegate?.arrowDownPressed()
}
#objc private func arrowUpPressed() {
customTextFieldDelegate?.arrowUpPressed()
}
}
3.In your ViewController change your current UITextField to your new MyCustomTextField and apply the Input Accessory View.
yourTextField.enableInputAccessoryView()
4.Set your ViewController to be the MyCustomTextField delegate.
yourTextField.customTextFieldDelegate = self
5.Confrom your ViewController to the newly created MyCustomTextFieldDelegate, where you will handle the user interaction.
extension YourViewController: MyCustomTextFieldDelegate {
func doneButtonPressed() {
// Handle done Pressed
}
func arrowDownPressed() {
// Handle down pressed
}
func arrowUpPressed() {
// Handle up pressed
}
}
Currently I am making pickerDate to interact with TextField.
class ViewController: UIViewController {
#IBOutlet weak var fromDateFilterTextField: UITextField!
#IBOutlet weak var toDateFilterTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
private func setupUI(){
fromDateFilterTextField.setInputViewDatePicker(target: self, selector: #selector(tapDone(_:)))
toDateFilterTextField.setInputViewDatePicker(target: self, selector: #selector(tapDone(_:)))
}
#objc func tapDone(textField : UITextField) {
}
}
extension UITextField{
func setInputViewDatePicker(target : Any , selector : Selector){
//create a UIDatePicker object and assign to inputView
let screenWidth = UIScreen.main.bounds.width
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 200))
datePicker.datePickerMode = .date
self.inputView = datePicker
//create a toolbar and assign it to inputAccesoryView
let toolBar = UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: screenWidth, height: 44.0))
toolBar.barTintColor = .red
datePicker.setValue(UIColor.black, forKeyPath: "textColor")
datePicker.setValue(false, forKeyPath: "highlightsToday")
datePicker.backgroundColor = .white
let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: nil, action: #selector(tapCancel))
cancel.tintColor = .white
let barButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector) //7
barButton.tintColor = .white
toolBar.setItems([cancel, flexible, barButton], animated: false)
self.inputAccessoryView = toolBar
}
#objc func tapCancel() {
self.resignFirstResponder()
}
}
I'd like to pass textfield as parameter of tapDone for reuse the code, because I'd like to update the date of differents textfield using only change the parameters of tapDone.
OK, I understand (generally) what you are trying to do. The short answer is that you can't pass your UITextField as an argument for the Selector executed by your UIBarButtonItem. That said, there are other options.
The simplest solution I can think of would be to use the tag attribute of your UITextFields and UIBarButtonItems. To set this up you would need to update the tag of each UITextField so that they are all unique. For example, your fromDateFilterTextField could have a tag of 0, the toDateFilterTextField could be 1. Just make sure they are all different from each other if you have more.
Once you have updated the tags on your textFields you can make sure the UIBarButtonItem has the same tag by modifying your setInputViewDatePicker method like so:
func setInputViewDatePicker(target : Any , selector : Selector){
...
let barButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector) //7
barButton.tintColor = .white
//Make the barButton tag match the associated textField's tag
barButton.tag = self.tag
...
}
Even though you can't pass the UITextField as an argument, you can pass the UIBarButton executing the Selector by changing your selector method like this:
#objc func tapDone(_ sender: UIBarButtonItem) {
}
Then you can determine which textField is associated with the tapDone by looking for that matching tag:
#objc func tapDone(_ sender: UIBarButtonItem) {
switch sender.tag {
case 0:
print("This is fromDateFilterTextField")
case 1:
print("This is toDateFilterTextField")
default:
print("This is a different textField")
}
}
You can also look into viewWithTag in the docs for possible improvements, though sometimes being really explicit makes your code more readable and less prone to bugs.
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(buttonClicked(sender:)))
#objc func buttonClicked(sender: UIBarButtonItem) {
print("Hello")
}
That's the code for my UIBarButton but when I click on it it doesn't print "Hello", what could be the problem?
EDIT: Here are my viewcontroller
It simply control a button that when it's clicked show the spinner with its control, but as I said before the button on toolbar doesn't work
class FilterViewController: UIViewController {
var search: Search?
let categoriesSpinnerDelegate = CategoriesPickerDelegate()
#IBOutlet weak var generalSpinner: UIPickerView!
#IBOutlet weak var categoriesButton: UIButton!
#IBOutlet weak var categoryRow: UIView!
var doneButton: UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.topViewController?.title = "Filtri"
// Set border and click action
self.categoryRow.layer.borderWidth = 1
self.categoryRow.layer.borderColor = Raccoltacase.lightGray.cgColor
self.categoryRow.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.buttonClicked(sender:))))
// Create toolbar and attach it to pickerView
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
//toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
self.doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(buttonClicked(sender:)))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton!], animated: false)
toolBar.isUserInteractionEnabled = true
generalSpinner.addSubview(toolBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func categoriesButtonClick(_ sender: UIButton) {
self.generalSpinner.showsSelectionIndicator = true
self.generalSpinner.dataSource = categoriesSpinnerDelegate
self.generalSpinner.delegate = categoriesSpinnerDelegate
self.generalSpinner.isHidden = false
}
#objc func buttonClicked(sender: UIBarButtonItem) {
print("Hello")
}
}
Screen
Assuming that you are showing the picker sometimes (and dismissing it when user presses the done button), here is my solution:
Add a UITextField to the view in storyboard (and make the tintColor transparent (clearColor))
Add the UIToolbar as inputAccessoryView to the UITextField
Add the UIPickerView as inputView to the toolbar (Also see the note below)
Below is a sample code:
override func viewDidLoad() {
super.viewDidLoad()
textField.inputView = generalSpinner
textField.inputAccessoryView = getToolbar()
}
func getToolbar() -> UIToolbar {
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 40))
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
self.doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(buttonClicked(sender:)))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton!], animated: false)
toolBar.isUserInteractionEnabled = true
return toolBar
}
Note: In case the UIPickerView is already in the storyboard (or a subview of another view), make sure to remove it in the first line of viewDidLoad (as shown below):
override func viewDidLoad() {
super.viewDidLoad()
generalSpinner.removeFromSuperview()
textField.inputView = generalSpinner
textField.inputAccessoryView = getToolbar()
}
SOLVED
When i use pickerView.addSubView(toolbar) the toolbar was placed behind the pickerview and so it was no clickable. I solved it adding the toolbar manually from storyboard with General Spinner.top = Picker Toolbar.bottom
I am trying with the below Swift code in my timer application to start and pause with the same button I have. But it is does not toggle. The button is set with Identifier as "Play" in the IDE. When I run the app, Timer starts and pauses correctly. But it does not toggle button. It always remain as "Play"
#IBAction func pressPausePlay(sender: AnyObject) {
if playPause == false
{
timer.invalidate()
self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "pressPausePlay")
playPause = true
}
else
{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pressPausePlay")
playPause = false
}
}
You can use this Example scenarios to toggle between Play and Pause, for Button Titles, Image, Navigation Bar, ToolBar
import UIKit
import AVFoundation
class ViewController: UIViewController {
var myPlayer = AVAudioPlayer()
#IBOutlet var myNaviBar: UINavigationBar! //OUTLET TO NAVIGATION BAR IMPORTANT TO WORK WITH ITS BARBUTTON ITEMS
#IBOutlet var myToolBar: UIToolbar!
//**************START :Bottom LEFT TOOLBAR Bar Button Item***********/
#IBAction func toolBarItemPlay(sender: AnyObject) {
myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "toolBarItemPause:")], animated: true)
myPlayer.play()
}
#IBAction func toolBarItemPause(sender: AnyObject) {
myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "toolBarItemPlay:")], animated: true)
myPlayer.pause()
}
//**************END :Bottom LEFT TOOLBAR Bar Button Item***********/
//**************START :Top LEFT Naivation Bar Button Item***********/
#IBAction func leftNavgationbarPlay(sender: UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "leftNavigationbarPause:")
myPlayer.play()
}
//no need of control drag to this one below because we are calling from the above leftNavgationbarPlay's Selector/action
#IBAction func leftNavigationbarPause(sender : UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "leftNavgationbarPlay:")
myPlayer.pause()
}
//**************END :Top LEFT Naivation Bar Button Item***********/
//**************START :Top Right Naivation Bar Button Item***********/
#IBAction func naviPlay(sender: UIBarButtonItem) {
//change the topright navigation bar item from SystemItem Play '>' to systemitem pause '||'
// here I am assigning the top left item Play to Bar button item Pause
/* ACTION SELECTOR IMPORTANT. IT MUST CALL THE IB ACTION FUNCTION OF PAUSE AND VICEVERSA, WITH ":" */
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "naviPause:")
myPlayer.play()
}
#IBAction func naviPause(sender: UIBarButtonItem) {
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "naviPlay:")
myPlayer.pause()
}
//**************END : Top Right Naivation Bar Button Item***********/
//************START :BUTTON IMAGE CHANGE - ">"to "||"************//
#IBAction func imagePlayButton(sender: UIButton) {
let chkImage = sender.currentImage!
if chkImage == UIImage(named: "play.png"){
sender.setImage(UIImage(named: "pause.png"), forState: .Normal)
myPlayer.play()
} else {
sender.setImage(UIImage(named: "play.png"), forState: .Normal)
myPlayer.pause()
}
}
//************END :BUTTON IMAGE CHANGE - ">"to "||"************//
//************START BUTTON TITLE CHANGE - PLAY to PAUSE************//
#IBAction func playButton(sender: UIButton) {
let chkTitle = sender.currentTitle
if chkTitle == "Play" {
sender.setTitle("Pause", forState: .Normal)
myPlayer.play()
} else {
sender.setTitle("Play", forState: .Normal)
myPlayer.pause()
}
}
//************END BUTTON TITLE CHANGE - PLAY to PAUSE************//
#IBAction func naviStop(sender: UIBarButtonItem) {
myPlayer.stop()
myPlayer.currentTime = 0
}
#IBOutlet var sliderUI: UISlider!
#IBAction func mySlider(sender: UISlider) {
myPlayer.volume = sliderUI.value
}
override func viewDidLoad() {
super.viewDidLoad()
let audioFilePath = NSBundle.mainBundle().pathForResource("ARRehman", ofType: "mp3")
//need NSURL from string
do {
myPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioFilePath!))
}
catch {
print("Some error")
}
}
}
You need to replace bar button items hold in UIToolbar after you assign a new instance to self.playPauseButtonVar.
If you have toolBar or navigationBar outlets:
self.toolBar.setItems([self.playPauseButtonVar], animated: true)
// Or
self.navigationBar.setItems([self.playPauseButtonVar], animated: true)
If your class inherits UIViewController:
self.setToolbarItems([self.playPauseButtonVar], animated: true)
// Or
self.navigationItem.setRightBarButtonItem(self.playPauseButtonVar, animated: true)
Yoichi, thanks for pointing me to the correct place. I removed the references on the Tool bar Outlet variable (bottomToolBar) under "Referencing Outlets". That resolved the error. However I am not able to add the tool bar outlet again, if I add, the error comes back!
it worked with me by creating bar button by code this is my code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationBar!
#IBOutlet weak var displayCountLabel: UILabel!
var buttonPause = UIBarButtonItem()
var buttonPlay = UIBarButtonItem()
var count=0
var timer=NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
buttonPause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseAction")
buttonPlay = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playAction")
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func stopTimer(sender: AnyObject) {
timer.invalidate()
count = 0
displayCountLabel.text = "\(count)"
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
}
func pauseAction() {
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
timer.invalidate()
}
func playAction() {
self.navigationBar?.topItem?.leftBarButtonItem = buttonPause
timer=NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("displayCount"), userInfo: nil, repeats: true)
}
func displayCount()
{
count++
displayCountLabel.text = "\(count)"
}
}
I need the navigation's back button always pops a specific UIViewController.
override func viewDidLoad() {
super.viewDidLoad()
//set title image
var logoImage:UIImage = UIImage(named: "barra")!
var logoImageView : UIImageView = UIImageView(image: logoImage)
logoImageView.frame = CGRectMake(0, 0, 320, 44)
logoImageView.contentMode = .ScaleAspectFit
logoImageView.contentMode = UIViewContentMode.Center
logoImageView.clipsToBounds = true
self.navigationItem.titleView = logoImageView
//set back image
var backImage:UIImage = UIImage(named: "freccia")!
backImage = backImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style: .Plain,target: self, action: "goToServizi:")
self.navigationController!.navigationBar.backIndicatorImage = backImage
self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = backImage
//set menu image
var menuImage:UIImage = UIImage(named: "menu")!
menuImage = menuImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationItem.rightBarButtonItem?.image = menuImage
}
func goToServizi(sender: UIBarButtonItem)
{
self.navigationController?.popToViewController(ServiziVC(), animated: true)// here I add a breakpoint, but it is never executed.
}
ServiziVC is the UIViewController that I need to show every time I click on the Back button.
I can't understand why goToServizi func is not called. Please help.
Thank you.
Here is the example for you.
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Customise your barButton Like this
var backImage:UIImage = UIImage(named: "freccia")!
backImage = backImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var backButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.Bordered, target: self, action: "goToThird")
self.navigationItem.leftBarButtonItem = backButton
}
func goToThird(){
//Initiate newViewController this way
let ThirdView = self.storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as ThirdViewController
self.navigationController?.pushViewController(ThirdView, animated: true)
}
}
EDIT
Click on the ViewController which you want to initiate with identifier and you can find StoryBoard ID in Identity Inspector like shown in below Image.
HERE is the example for you for more reference.
Modify code as per your need.may be this will help you.
self.navigationController?.popToViewController(ServiziVC(), animated: true)You can't customize the back button action like that. This works:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true;
var backImage:UIImage = UIImage(named: "freccia")!
backImage = backImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.Bordered, target: self, action: "goToServizi:")
self.navigationItem.leftBarButtonItem = fakeBackButton;
}
func goToServizi(sender: UIBarButtonItem)
{
self.navigationController?.popToViewController(ServiziVC(), animated: true)
}
Alternatively, you could override the UINavigationController's delegate method as described here : UINavigationController and back button action