n00b please help button not working

so im a student and im cloning an app just to play around with Swift. I have a view that contains a button that, when pressed, goes to a different view with a sheet presentation. I was to add another button on that sheet view that also goes to another view but full screen. so first view button -> sheet button -> last view

the first button works fine, but on the sheet the button won't even print anything to the console when pressed. The first button (that works), was only added programmatically because I had an image in the background of the view with a fake button look, so I did not add a button with a frame or CG.

im guessing that is my issue since for the sheet button I am adding the button programmatically and the UI too.

this is the code for the button that doesnt work //cashout button cashOutBut.configuration = .filled() cashOutBut.configuration?.baseBackgroundColor = .systemGreen cashOutBut.configuration?.cornerStyle = .large cashOutBut.titleLabel?.text = String("Cash Out") cashOutBut.configuration?.title = String("Cash Out") cashOutBut.isEnabled = true //var config = UIButton.Configuration.filled() //config.baseBackgroundColor = .green view.addSubview(cashOutBut)

}

@IBAction func cashOutBut(_sender: Any){
    let detailViewController = CashOutViewController()
    let nav = UINavigationController(rootViewController: detailViewController)
    nav.modalPresentationStyle = .fullScreen
    (nav, animated: true, completion: nil)
}

the one that works is not added to the view/subview and is only

@IBAction func cashOut(_ sender: Any){
    let detailViewController = SheetViewController()
    let nav = UINavigationController(rootViewController: detailViewController)
    nav.modalPresentationStyle = .pageSheet

    if let sheet = nav.sheetPresentationController {
        sheet.detents = [.medium(), .large()]
        sheet.prefersScrollingExpandsWhenScrolledToEdge = false
        sheet.preferredCornerRadius = 25// add
        sheet.prefersGrabberVisible = true
    }
    present(nav, animated: true, completion: nil)
}

do I need to add a target or someting?

Welcome to the forum.

Let's first edit code properly:

// cashout button
cashOutBut.configuration = .filled()
cashOutBut.configuration?.baseBackgroundColor = .systemGreen
cashOutBut.configuration?.cornerStyle = .large
cashOutBut.titleLabel?.text = String("Cash Out")
cashOutBut.configuration?.title = String("Cash Out")
cashOutBut.isEnabled = true
//var config = UIButton.Configuration.filled() //config.baseBackgroundColor = .green view.addSubview(cashOutBut)

Your code and explanations are not clear:

  • what is this IBAction ? When added programmatically ?
  • Why call func also cashOutBut ?
  • what does the last line mean (I guess you edited and removed present) ?
@IBAction func cashOutBut(_sender: Any){
    let detailViewController = CashOutViewController()
    let nav = UINavigationController(rootViewController: detailViewController)
    nav.modalPresentationStyle = .fullScreen
    (nav, animated: true, completion: nil)
}
  • Is the next one added in storyboard ?
@IBAction func cashOut(_ sender: Any){
    let detailViewController = SheetViewController()
    let nav = UINavigationController(rootViewController: detailViewController)
    nav.modalPresentationStyle = .pageSheet

    if let sheet = nav.sheetPresentationController {
        sheet.detents = [.medium(), .large()]
        sheet.prefersScrollingExpandsWhenScrolledToEdge = false
        sheet.preferredCornerRadius = 25// add
        sheet.prefersGrabberVisible = true
    }
    present(nav, animated: true, completion: nil)
}

To create programmatically, you have to:

  • set the target
cashOutBut.addTarget(self, action: #selector(cashOut_:)), for: UIControl.Event.touchUpInside)
  • define the selector in target as an @objC func:
@objc func cashOut(_ sender : UIButton) {  // I change the func name to cashOut, to be different from button's name
 }
  • and called as (if that's the right button name):
n00b please help button not working
 
 
Q