UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 54 - project 13 (Button's title / chosen filter)

Forums > 100 Days of Swift

Hi 100DaysOfSwift Maniacs :)

I've faced a problem with second challenge in which we need to pass a name of a chosen filter and change a button's title. Probably it's very simple but maybe anyone could point out where I can pass this name ? I've tried some solutions but none of them worked...

 @IBAction func changeFilter(_ sender: Any) {
    let ac = UIAlertController(title: "Choose filter", message: nil, preferredStyle: .actionSheet)
    ac.addAction(UIAlertAction(title: "CIBumpDistortion", style: .default, handler: setFilter))
    ac.addAction(UIAlertAction(title: "CIGaussianBlur", style: .default, handler: setFilter))
    ac.addAction(UIAlertAction(title: "CIPixellate", style: .default, handler: setFilter))
    ac.addAction(UIAlertAction(title: "CISepiaTone", style: .default, handler: setFilter))
    ac.addAction(UIAlertAction(title: "CITwirlDistortion", style: .default, handler: setFilter))
    ac.addAction(UIAlertAction(title: "CIUnsharpMask", style: .default, handler: setFilter))
    ac.addAction(UIAlertAction(title: "CIVignette", style: .default, handler: setFilter))
    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
    present(ac, animated: true)
  }

I need to pass chosen title from UIAlertAction 'title' to change title of a button

Thank you in advance.

3      

So, the setFilter function should have a signature somewhat like this, right?

func setFilter(action: UIAlertAction) {...

The UIAlertAction here is the button that you press, which has the name of the filter as its title. So, all you need to do is get the title from the button and set it as the other button's title.

selectFilterButton.setTitle(action.title, for: .normal)

3      

That was first thing that I've tried however this doesn't work... I added Outlet for this button:

  @IBOutlet var imageView: UIImageView!
  @IBOutlet var intensity: UISlider!
  @IBOutlet var changeFilterOutlet: UIButton!

and added setTitle within setFilter function:

 private func setFilter(action: UIAlertAction){
    guard currentImage != nil else {return}
    guard let actionTitle = action.title else {return}
    currentFilter = CIFilter(name: actionTitle)
    changeFilterOutlet.setTitle(action.title, for: .normal)
    let beginImage = CIImage(image: currentImage)
    currentFilter.setValue(beginImage, forKey: kCIInputImageKey)
    applyProcessing()
  }

Unfortunately title is not changing when I'm tapping on filter....

3      

That's weird. I've actually been experiencing stuff like that. Things that were supposed to work are doing nothing or behaving wrongly now. Maybe this is a bug in UIKit. Maybe wait for the next Xcode update and test it again.

3      

AAAA. I know what caused that problem.... I was trying to change button's title after guard let image without choosing an image....

func setFilter(action: UIAlertAction){
    guard currentImage != nil else {return}
    guard let actionTitle = action.title else {return}
    currentFilter = CIFilter(name: actionTitle)
    changeFilterOutlet.setTitle(action.title, for: .normal)
    let beginImage = CIImage(image: currentImage)
    currentFilter.setValue(beginImage, forKey: kCIInputImageKey)
    applyProcessing()
  }

Solution was to place changeFilterOutlet.setTitle(action.title, for: .normal) before guard let statement. I this case I'm able to change button's name even when image is not chosen!

func setFilter(action: UIAlertAction){
    changeFilterOutlet.setTitle(action.title, for: .normal)
    guard currentImage != nil else {return}
    guard let actionTitle = action.title else {return}
    currentFilter = CIFilter(name: actionTitle)
    let beginImage = CIImage(image: currentImage)
    currentFilter.setValue(beginImage, forKey: kCIInputImageKey)
    applyProcessing()
  }

5      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.