# Losing Hair - UIBarButton won't work after dismissing ViewController

**URL:** <https://forums.kodeco.com/t/losing-hair-uibarbutton-wont-work-after-dismissing-viewcontroller/11446>\
**Category:** iOS\
**Created:** [March 30, 2016, 5:39am UTC](https://forums.kodeco.com/t/losing-hair-uibarbutton-wont-work-after-dismissing-viewcontroller/11446 "2016-03-30T05:39:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emanleet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/emanleet/32/103225_2.png) [@emanleet](https://forums.kodeco.com/u/emanleet)\
**Post date:** [March 30, 2016, 5:39am UTC](https://forums.kodeco.com/t/losing-hair-uibarbutton-wont-work-after-dismissing-viewcontroller/11446/1 "2016-03-30T05:39:49Z")

</div>

So if the image picker appears and I cancel, everything is fine. However, when I choose an image, and the image picker view dismisses, I can’t select the “pick” button again, it won’t work or print anything to the console. I have no idea why. Here’s my code:

`class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {  
@IBOutlet weak var imagePickerView: UIImageView!

```
@IBAction func pickAnImage(sender: AnyObject) {
    selectImageSourceAlert()
    print("test")
}

override func viewDidLoad() {
    super.viewDidLoad()
}

func selectImageSourceAlert() {
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
    let userLibrary = UIAlertAction(title: "Photo Library", style: .Default) {
        _ in
        self.presentImagePicker(pickerStyle: .PhotoLibrary)
    }
    let useCamera = UIAlertAction(title: "Camera", style: .Default) {
        _ in
        // TODO: Check if camera isn't available, skip action
        let x = UIImagePickerController.isSourceTypeAvailable(.Camera)
        print(x)
        if x {
            self.presentImagePicker(pickerStyle: .Camera)
        } else {
            print("No camera available")
        }
        
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    
    alert.addAction(userLibrary)
    alert.addAction(useCamera)
    alert.addAction(cancelAction)
    
    presentViewController(alert, animated: true, completion: nil)
}

func presentImagePicker(pickerStyle style: UIImagePickerControllerSourceType) {
    let imagePicker = UIImagePickerController()
    imagePicker.sourceType = style
    imagePicker.allowsEditing = true
    imagePicker.delegate = self
    
    self.presentViewController(imagePicker, animated: true, completion: nil)
}

// MARK: - ImagePicker Protocol

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    
    if let image = info["UIImagePickerControllerEditedImage"] as? UIImage {
        imagePickerView.image = image
    }
    
    dismissViewControllerAnimated(true, completion: nil)
}

```

}

`

Hope you guys can help as I’ve been coming back to this for the past 2 days and still can’t figure it out.

---

<div class="post-metadata">

**Author:** ![emanleet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/emanleet/32/103225_2.png) [@emanleet](https://forums.kodeco.com/u/emanleet)\
**Post date:** [March 30, 2016, 7:15am UTC](https://forums.kodeco.com/t/losing-hair-uibarbutton-wont-work-after-dismissing-viewcontroller/11446/2 "2016-03-30T07:15:19Z")

</div>

I don’t know if this will help, but when I remove this code:

`if let image = info["UIImagePickerControllerEditedImage"] as? UIImage { imagePickerView.image = image }`

Everything works again. But when I add it back, the button’s function doesn’t work again.

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [March 30, 2016, 8:17am UTC](https://forums.kodeco.com/t/losing-hair-uibarbutton-wont-work-after-dismissing-viewcontroller/11446/3 "2016-03-30T08:17:46Z")

</div>

Using your class and with a simple storyboard everything went fine. Can we have a screenshot of your storyboard or even of your app in your device ?

---

<div class="post-metadata">

**Author:** ![emanleet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/emanleet/32/103225_2.png) [@emanleet](https://forums.kodeco.com/u/emanleet)\
**Post date:** [March 30, 2016, 2:00pm UTC](https://forums.kodeco.com/t/losing-hair-uibarbutton-wont-work-after-dismissing-viewcontroller/11446/4 "2016-03-30T14:00:51Z")

</div>

Update:

So I added a regular button at the top right corner and that one works even after adding an image. My suspicion is my first button doesn’t work because it is a UIBar Button Item. Here’s an image that might be helpful (made the image background red for clarity):

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/2X/1/129af353d12bb33854138018b785457863058d41.png)

Any idea how to fix it with these additional insight?

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [April 1, 2016, 8:29am UTC](https://forums.kodeco.com/t/losing-hair-uibarbutton-wont-work-after-dismissing-viewcontroller/11446/5 "2016-04-01T08:29:28Z")

</div>

Well, that is pretty weird to have the `UIBarButtonItem` disabled… If your project is not too personal or if you can reduce the issue to a test subject with only this ViewController, upload it and I’ll give it a look
