UIBarButtonItem not triggering IBAction - swift

I'm trying to create a UIBarButtonItem that opens the camera once the button has been tapped. For some reason, my takePicture(_sender:) function doesn't seem to be getting called.
I originally tried to create my UIBarButtonItem using the Interface Builder. Here was the interface and a screenshot of the actions connected to the UIBarButtonItem:
And here is the code for my takePicture(_sender:) function:
#IBAction func takePicture(_ sender: UIBarButtonItem) {
print("Taking picture...")
let imagePicker = UIImagePickerController()
// If the device has a camera, take a picture; otherwise,
// just pick from photo library
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
} else {
imagePicker.sourceType = .photoLibrary
}
imagePicker.delegate = self
// Place image picker on the screen
present(imagePicker, animated: true, completion: nil)
}
The little circle next to my function declaration is filled in and connected properly:
However, when I load the simulator and press the button, the UIImagePickerController never appears and my print() function is never called in the code.
So, I then tried to declare the UIBarButtonItem programmatically to see if perhaps it was an issue under-the-hood of Xcode's Interface Builder. Here was my code:
(Note: I deleted the UIBarButtonItem from the Interface Builder and then I connected the UIToolbar to my code using an #IBOutlet.)
override func viewDidLoad() {
super.viewDidLoad()
let takePictureBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: self, action: #selector(DetailViewController.takePicture))
toolBar.setItems([takePictureBarButton], animated: false)
}
#objc func takePicture() {
print("Taking picture...")
let imagePicker = UIImagePickerController()
// If the device has a camera, take a picture; otherwise,
// just pick from photo library
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
} else {
imagePicker.sourceType = .photoLibrary
}
imagePicker.delegate = self
// Place image picker on the screen
present(imagePicker, animated: true, completion: nil)
}
As a last ditch attempt at debugging this program and trying to find where the problem lies, I created a temporary button in my UI and connected that to my takePicture(_sender:) function instead (I changed the signature from a UIBarButtonItem to a UIButton). That worked perfectly. This tells me that the problem is not with the function itself, but has something to do with the connection.

I was seconds away from posting this question when I found a similar question. The user stated that the root of their problem lied in a UITapGestureRecognizer on their View Controller.
To solve my problem, I simply set my UITapGestureRecognizer's target to be my StackView instead of the the entire view.
Original:
Resolved:
I'd be curious to learn why the UITapGestureRecognizer stopped my UIBarButtonItem from being tapped, but not the normal UIButton. Seems a little strange...

Swift 4
I went through this problem and referenced above answers. The project I worked on had UIButton as navigation bar item and hence it comes under navigation item and I found out that the outlet for the IBAction should be connected to the button in the bar button item rather than connecting it to the bar button item itself. In mattkx4's question, the IBAction has 'UIBarButtonItem' as its sender. I fixed the issue by connecting the button inside the bar button item to the action.
As you can see there is a button inside the bar button item. Connect the action to this button and the action will be triggered.

make sure you connect your camera button with action for touchUpInside event

Related

How can I fix my navigation bar back button?

Hi I've been trying to change my back button in my application's navigation bar. The problem is that I see the change when I load the view twice.
I've searched for the answer several times but I don't see what I really want. In fact, I'm new at this language so it's difficult.
What I tried is putting the following lines in the viewWillAppear method and others*:
nav?.navigationBar.backItem?.title = "Messages"
The result is that when I enter the view I see the back button's title as Back. Then if I press on that button and enter the view again the title changes as I want. On the other hand, what I want is to change the title when I load the view first.
*-> I've tried the same line in viewDidLoad too see if that does anything and in viewWillDisappear of the previous view, but nothing happens.
You are using method a change back button in your current ViewController it is wrong, because your navigationBar configured in past controller. If you use Storyboard, please will try this method in your parent controller:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let backItem = UIBarButtonItem()
backItem.title = "Messages"
navigationItem.backBarButtonItem = backItem
}
If you init newController in your parentController, you must to modify your a backButton in parentController, example:
// It's action for any button witch present your newController
#objc private func presentDetailViewContorller() {
let weatherDetailController = WeatherDetailController()
let navController = NavigationController(rootViewController: weatherDetailController)
let backItem = UIBarButtonItem()
backItem.title = "Messages"
navController.backBarButtonItem = backItem
self.present(navController, animated: true, completion: nil)
}
Good luck!

Using UINavigationItem on UITabBarController

I have a project with a UITabBarController and a couple of views. Sort of like this:
In this project, the tab bar controller is called by tapping the Tab button on the UIViewController. I would like to get rid of the back button with "Title", and replace it with an "X" icon. When tapped, the "X" icon would dismiss the UITabBarController and return to the caller. I do this all of the time on UINavigationController using a UINavigationItem, but that does not appear to work in this situation. I drag the UINavigationItem to the view and it allows it, but it does not show up on the view and any UIBarButtonItem that I drag and drop on it do not appear.
Is there a way to actually do this? I'd even be ok with leaving the existing back button as it is and just getting rid of "Title"
I figured it out right after posting the question. Just a bit more research is all it took.
To fix this, add the following to the UITabBarController code.
override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: "back")
let leftButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(dismissTabBar))
leftButton.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftButton
}
#IBAction func dismissTabBar() {
self.navigationController?.popToRootViewController(animated: true)
}
This gives me a black back button with my image. When I tap it, it brings me back to the calling 'UIViewController.

Searchbar cancel button not visible only on iPad

First I want to say I didn't find any good answer about it in Swift. I created search bar. On button click it shows and on cancel it hides the searchbar. It is working properly, cancel button is visible on all iPhones but for some reason not on iPad. What should cause this?
That is how I create the searchbar:
//Create searchbar
func createSearchBar(){
searchBar.showsCancelButton = true
searchBar.tintColor = UIColor(red:0.184, green:0.996, blue:0.855, alpha:1.00)
searchBar.placeholder = "Search brands"
searchBar.delegate = self
searchBar.hidden = false
searchBar.alpha = 0
navigationItem.titleView = searchBar
navigationItem.setLeftBarButtonItem(menuButton, animated: true)
navigationItem.setRightBarButtonItem(searchButtton, animated: true)
UIView.animateWithDuration(0.5, animations: {
self.searchBar.alpha = 1
}, completion: { finished in
self.searchBar.becomeFirstResponder()
})
}
Faced the same one of my project. I've posted on apple forum and lot of developers commented as a xcode bug. So I added the cancel button manually for ipad views
func configureCancelBarButton() {
let cancelButton = UIBarButtonItem()
cancelButton.image = UIImage(named: "cancelButton")
cancelButton.action = #selector(viewController.ipadCancelButton(_:))
cancelButton.target = self
self.navigationItem.setRightBarButtonItem(cancelButton, animated: true)
}
And I previously posted an answer about this question. Check that too. :)
https://developer.apple.com/documentation/uikit/uisearchbar/1624283-showscancelbutton
The value of this property is ignored, and no cancel button is displayed, for apps running on iPad.
Apple add that in documents!
The UISearchbar's cancel button will work in iPads if you are NOT using the UISearchbar as NavigationBarItem.

How does presentViewController display the view?

I'm curious as to what the presentViewController does with the first parameter, imagePickerController. Given the way I assigned imagePickerController's properties, will the presentViewController use all that information to display the view?
#IBAction func imageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// Hide the keyboard.
myTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
presentViewController won't use any of the options. The imagePickerController you passed in will use those options.
Since the Apple APIs are pretty well designed, presentViewController does one thing (SRP), and does exactly what it says it does... presents the view controller you pass it.
The imagePickerController is in charge controlling the view of the image picker.

Perform segue and don't show "Back" button on the destination view controller

I would like to seque programmatically to a "setup-VC" when the initial setup is not done. This works, BUT in this case I don't want to show the back button on the "setup-VC".
What I've done till now:
I've created two VC. (main-VC and setup-VC)
The code of main-VC:
...
if InitialSetupIsDone == true {
println("Loading the data...") //PPP
} else {
println("Segue to setup screen...") //PPP
self.performSegueWithIdentifier("segueToSetup", sender: self)
}
...
// Pull any data from the view controller which initiated the unwind segue.
#IBAction func segueToSetup(sender: UIStoryboardSegue)
{
let sourceViewController: AnyObject = sender.sourceViewController
}
In the main-VC I've created an unwind-segue ("EXIT"-action? - red icon at the top of the VC) and named it "segueToSetup".
At last point I've connected the "setup-VC" with the "main-VC" while dragging an segue from main-VC (yellow Icon top left) to the setup VC.
Then it would look like this:
(The upper connection goes directly from setup sign to the "setup-VC")
The goal for me now would be to kind of "hide" the back button in "setup-VC" while the initial setup is not done. Maybe I've don't create the unwind segue right... !? Thx
Write this line in destination VC viewDidLoad
navigationController?.navigationItem.setHidesBackButton(true, animated: true)
In viewWillAppaer method also write this
navigationController?.navigationItem.setHidesBackButton(true, animated: true)
This disabled the button for me:
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton