UIBARBUTTON back action - ios

let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: Selector(("HomeTabController")))
self.navigationItem.leftBarButtonItem = backButton
The above code is the creation of button in navigationController but I can create a button cannot write a specific view controller to open.
I have tried with popViewController and popToRootViewController action, need a specific code for opening a particular viewController in swift, with the help of the particular viewController storyboard id and viewcontrollername.

You need to add func name in #selctor() (Swift 4 version)
let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goToViewController(_:)))
self.navigationItem.leftBarButtonItem = backButton
You need to create a func.
#objc func goToViewController(_ sender: UIBarButtonItem) {
//write code here to open a view controller
let storyboard = "Main"
let viewControllerIdentifier = "HomeVC"
let viewController = UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: viewControllerIdentifier) as! HomeVC
//push/present "viewController"
}

in this code just replace ChatVC name to your viewcontroller name
#objc func goToViewController(_ sender: UIBarButtonItem) {
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ChatVC.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
} else {
self.navigationController?.popViewController(animated: true)
}
}
}

Related

RightBarButtonItem not showing up, when leftBarButtonItem works fine

In my app I have a push segue from HomeViewController to EditProfileViewController which should have a back button as a leftBarItem and a settings cog as a rightBarItem. The back button displays normally, but the right item is missing. These ViewControllers live happen in the MainNavigationController which has a Navigation Bar.
I tried to define the rightBarButton in ViewDidLoad of the EditProfileVC, I also tried to have a rightBarItem in the storyboard for the View controller.
let buttonItem = UIBarButtonItem(image: settingsIcon, style: .plain, target: self, action: #selector(settingsPressed))
buttonItem.tintColor = UIColor(.settingsIconTint)
navigationItem.rightBarButtonItem = buttonItem
Interestingly if I change the rightBar to a leftBar item, the back button is replaced with the settings cog and works as I expect, but I can't go back to the main page.
let buttonItem = UIBarButtonItem(image: settingsIcon, style: .plain, target: self, action: #selector(settingsPressed))
buttonItem.tintColor = UIColor(.settingsIconTint)
navigationItem.leftBarButtonItem = buttonItem
To set a rightBarButtonItem in a navigationBar,
class HomeViewController: UIViewController {
#IBAction func openEditVC(_ sender: UIButton) {
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EditProfileViewController") as? EditProfileViewController {
self.navigationController?.pushViewController(controller, animated: true)
}
}
}
class EditProfileViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let buttonItem = UIBarButtonItem(title: "Settings", style: .plain, target: self, action: #selector(settingsPressed))
buttonItem.tintColor = .red
navigationItem.rightBarButtonItem = buttonItem
}
#objc func settingsPressed() {
print("Setting Pressed")
}
}
In the above code I've added a UIBarButtonItem with title Settings as a rightBarButtonItem of navigationBar.
No need to configure leftBarButtonItem it not required. Back button is added by default.
Screenshot:
In case it doesn't satisfy your requirement, add a screenshot of what is expected so I can help.

How to present UI view and keep Tab bar in Swift

I can not succeed in presenting a UI view and keep the Tab bar in Swift. It does successfully present but there is no tab bar. It appears that presenting a UIView removes the tab bar. Here is my code.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goMessages))
Here is what I use.
class MessagesController: UITableViewController {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goMessages))
let image = UIImage(named: "new_message_icon")
navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(handleNewMessage))
checkIfUserIsLoggedIn()
tableView.register(UserCell.self, forCellReuseIdentifier: cellId)
// observeMessages()
}
#objc func goMessages() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let Messages = storyBoard.instantiateViewController(withIdentifier: "Messages") as! MainMSGController
self.present(Messages, animated:true, completion:nil)
You have to push the view controller inside your navigationViewController.
But if you do not have one you should embed your MessagesController inside UINavigationViewController.
(inside your Main.storyboard, select MessagesController then from menu Editor -> Embed in -> Navigation Controller)
and then:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let Messages = storyBoard.instantiateViewController(withIdentifier: "Messages") as! MainMSGController
navigationController?.pushViewController(viewController: Messages, animated: true)
to close it you can:
navigationController?.popViewController(animated: true)
You can't present view controller and keep the tabs. You can only push the view controller if you want to do that. There are some other things required but at the first glance they seem to be implemented properly in your code.

How to present popUp when user taps on right bar button item?

I'm relatively new at iOS. I'm struggling, for a while now with this issue. I don't know how to set the logic for presenting popUp when user taps on right bar button item. Basically, it should look like this:PopUp I searched through Google, but I didn't have any luck. I would appreciate if someone would help me with some code.
//My VC
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let rightBarButtonItem = UIBarButtonItem(title: "Share", style: .plain, target: self, action: #selector(clickShare))
rightBarButtonItem.tintColor = UIColor.black
navigationItem.rightBarButtonItem = rightBarButtonItem
// navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Share", style: UIBarButtonItemStyle.plain, target: self, action: #selector(clickShare))
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "backArrow"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(goBack))
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: "OpenSans", size: 14)! ], for: .normal)
}
//MARK: - Actions
#objc func goBack() {
navigationController?.popViewController(animated: true)
}
#objc func clickShare() {
//this is where the logic should go
}
//This is my storyboard:
Storyboard
Add following action,
#IBAction func yourButtonClickAction(sender: UIBarButtonItem) {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("YourViewController") as! UIViewController
vc.modalPresentationStyle = UIModalPresentationStyle.Popover
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.barButtonItem = sender
popover.delegate = self
presentViewController(vc, animated: true, completion:nil)
}
Add this action to you barButtonItem.
'YourViewController' = controller consisting of Public, followers options.

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

performSegue with programmatically added Navigation button

I am trying to customize a Navigation bar button item programmatically... I am just having some issues here which I am sure is an easy fix. I want to make it the default Add button for now, but also in the future will want to make the bar button item a custom icon I create. so I guess I would like to know both ways how to programmatically change a navigation bar button to default styles and custom icons...thanks!
override func viewDidAppear(animated: Bool) {
let rightbutton = UIBarButtonItem(title: "+", style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked")
navigationItem.rightBarButtonItem = rightbutton
}
Here is how you would do it with the default add button:
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "uploadButtonClicked")
self.navigationItem.rightBarButtonItem = button
Here is how you would programmatically add a custom image:
let customImage = UIImage(named: "customButtonImage")
let customButton = UIBarButtonItem(image: customImage, style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked:")
self.navigationItem.rightBarButtonItem = customButton
To perform the segue, you can do this:
#IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
performSegueWithIdentifier("segueIdentifier", sender: self)
}
Or, if you want to go to another storyboard from there, then your IBAction will look like this:
#IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let nc = storyboard.instantiateInitialViewController() as! UINavigationController
let vc = nc.viewControllers.first as! YourViewController

Resources