Swift: Edit Mode, link editButtonItem() to IBAction - ios

I understand how to set my UITableView into edit mode, and how to dynamically create an edit button:
override func viewDidLoad() {
tableView.allowsMultipleSelectionDuringEditing = true
tableView.setEditing(false, animated: false)
navigationItem.leftBarButtonItem = editButtonItem()
}
But when I tap the edit button, I would like a new button to appear on the navigation bar (i.e. a 'plus'/'add' button). To do this I think I need to create an IBAction, but I don't know how to link the editButtonItem() to an action. Any ideas?

Ok, big thanks to Ahmed and vadian for their comments, but what I got working was this:
override func setEditing(editing: Bool, animated: Bool) {
// Toggles the edit button state
super.setEditing(editing, animated: animated)
// Toggles the actual editing actions appearing on a table view
tableView.setEditing(editing, animated: true)
if (self.editing) {
navigationItem.rightBarButtonItem =
UIBarButtonItem(barButtonSystemItem: .Add, target: self,
action: #selector(clickMe))
} else {
// we're not in edit mode
let newButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.rightBarButtonItem = newButton
}
}
func clickMe()
{
print("Button Clicked")
}
As the edit button is pressed (and flips from Edit -> Done and back again) the code in the IF/ELSE statements will execute.

You can replace the default action of editButtonItem() by assigning a new function defined in your view controller to its action property.
editButtonItem().action = #selector(yourCustomAction(_:))
func yourCustomAction(sender: UIBarButtonItem) {}

Related

Swift 5 - How to hide back button in Navigation bar or move to another screen without back button

I was having this issue and I've tried a lot of solutions that was proposed by some kind people here in the following topic:
Swift - How to hide back button in navigation item
I created a ViewController class:
import SwiftUI
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
}
and AS you can see in the above code I tried every single way with no change - back button still appear - then I try to make simple change like change the text of the back button or the shape and also there is no result!!
Am I do something wrong :( Because I feel like the whole class is not active for my view
Do I need to create an object of ViewController or something like that? Because I just wrote the mentioned code about my view code.
MY GOAL: I just want to move from view to another with no back button if there is another way I wouldn't mind to do it.
PPLLLSSSS HELPP ME Guys I'm so tired, I'll work on another things until find a solution for that and I'm sure there is a lot of people who want a solution for that issue.
Once I find the solution I'll share it with you guys :) Best Wishes and Regards
You just have to add the below code in the ViewController where you want to hide the backbutton.
navigationItem.setHidesBackButton(true, animated: true)
Segue from viewController to viewController2 and name the segue testSegue. This should work.
I had the exact same problem and the only solution that worked was adding
.navigationBarBackButtonHidden(true) in my SwiftUI view

How to go back from one viewcontroller to other viewcontroller

I have a simple one for you guys. I have created a back button on a UITableviewController, and want to set that back button to my other UIViewController(NewTableViewController).
here i implemement back button
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(NewTableViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
and action is
func back(sender: UIBarButtonItem) {
// perform your custom action
//....
// go back to the previuos view controller
_ = navigationController?.navigationController?.self.dismiss(animated: true)
}
but i am unable to go back
Thanks for the help you guys. Please tell me what I am missing.
Try this for swift 3
If you use model view controller
func back(sender: UIBarButtonItem) {
self.dismiss(animated: true, completion: {});
}
OR
If you use push
func back(sender: UIBarButtonItem) {
if let navController = self.navigationController {
navController.popViewController(animated: true)
}
}

Inside tabbar viewcontrollers navigationbar changes not working from storyboard

I have created
NavigationController(Main) - > LoginViewController -> Tabbarviewcontroller -> HomeViewController
If I add barbutton item in HomeViewController through storyboard it's not displaying in simulator.
But I can see the changes in storyboard.
Title Home1 and barbutton item not displaying in simulator
Try this Code: Tested in Swift 3:
Note: Delete all your barButtonItems and try below code.
Add this code to your Home1 VC:
override func viewWillAppear(_ animated: Bool) {
let RightButtomitem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = RightButtomitem
}
func handler(sender:UIButton) {
print("Add Button pressed")
}
Add this code to your Home2 VC:
override func viewWillAppear(_ animated: Bool) {
let RightButtomitem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = RightButtomitem
}
func handler(sender:UIButton) {
print("Done Button Pressed")
}
Output:
First you hide Navigation Bar in NavagationController and you make custome bar After you Add in it which you want in bar .
You can try it programmatically
HomeViewController ---
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
let barButtomitem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = barButtomitem
}
SecondItemViewController--
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
self.tabBarController?.navigationItem.rightBarButtonItem = nil
}
and don't add barbutton in storyboard ...

UINavigationBar show Back button after interrupt swiping right from left edge gesture, how to hide it and why it shown?

In viewDidLoad, i configure the navigationBar with:
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationItem.setHidesBackButton(true, animated: false)
let backButton:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "nav_back_normal"), style: UIBarButtonItemStyle.Done, target: self, action: #selector(ProfileViewController.backTapped(_:)))
self.navigationItem.setLeftBarButtonItems([backButton], animated: true)
and in previous view controller's viewDidAppear,
self.navigationController?.navigationBarHidden = true
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
And the gesture delegate is :
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if self.navigationController?.viewControllers.count > 1 {
return true
}else {
return false
}
}
when i want to swipe right to back to the previous controller, and interrupt, the blue tint Back button shown.
So, why it show and how to disable it?
I add one line of code to solve this problem in viewDidAppear of current view controller.
self.navigationController?.navigationBar.backItem?.backBarButtonItem = nil

How do I create a cancel button?

I am having troubles getting my cancel button to work. I have this action connected to my cancel button and I plan to dismiss the view controller like so:
#IBAction func cancel(sender: UIBarButtonItem) {
dismissViewControllerAnimated(true, completion: nil)
}
I was wondering what obvious bit I am missing. (This is a bar button item for a table view).
var b = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "Cancel clicked")
If you wanted the method to take the sender as a parameter, you would put a colon at the end:
var b = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "CancelClicked:")
func CancelClicked(sender: UIBarButtonItem) {
}
After you add the NavigationBar (and Navigation Item) to the Storyboard using IB, the reference to navigationItem var will be automatically mapped to your ViewController file. Add the code below to attach a cancel button to the navigationItem to dismiss the modal View Controller:
#IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelClicked(sender:)))
}
func cancelClicked(sender: UIBarButtonItem) {
print("Cancel clicked!")
self.dismiss(animated: true, completion: nil)
}
Try this:
#IBAction func close(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Your code works fine with one little fix. Dismiss is called on the presenting controller. self.dismissViewController would dismiss child view controller presented by the current vc. but if you wanna dismiss the existing vc you should call dismiss on the parent
#IBAction func cancel(sender: UIBarButtonItem) {
presentingViewController.dismissViewControllerAnimated(true, completion: nil)
}

Resources