UIButton не работает. Вызывает краш приложения


#1

Странное дело творится товарищи.

Попробовал создать программно простенькую кнопку. При попытке нажать получаю фатал “[UIButton start:]: unrecognized selector sent to instance 0x144d09c70”

Вот код:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    let viewController = ViewController()
    
    let navigationController = UINavigationController(rootViewController: viewController)
    navigationController.navigationBar.isTranslucent = false
    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()
    
}

func sceneDidDisconnect(_ scene: UIScene) {}

func sceneDidBecomeActive(_ scene: UIScene) {}

func sceneWillResignActive(_ scene: UIScene) {}

func sceneWillEnterForeground(_ scene: UIScene) {}

func sceneDidEnterBackground(_ scene: UIScene) {}

}

class ViewController: UIViewController {

let startButton = UIButton(type: .custom)

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.view.backgroundColor = .white
    
    startButton.frame = CGRect(x: 24, y: 160, width: UIScreen.sWidth - 48, height: 50)
    startButton.setTitle("Добавить", for: .normal)
    startButton.setTitleColor(.blue, for: .normal)
    startButton.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.0)
    startButton.layer.cornerRadius = 8
    startButton.layer.borderWidth = 1.0
    startButton.layer.borderColor = CGColor.init(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0)
    startButton.addTarget(startButton, action: #selector(start(_:)), for: .touchUpInside)
    self.view.addSubview(startButton)
}

@objc func start(_ sender: UIButton?) {
    print("Button pressed")
}

}

Совсем голову растерял. Кто нибудь сталкивался с подобным?

Буду благодарен за подсказки

Ссылка на ГитХаб https://github.com/VKostin8311/Crazy-Counter


#2

startButton.addTarget(startButton, action: #selector(start(_:)), for: .touchUpInside)
->
startButton.addTarget(self, action: #selector(start(_:)), for: .touchUpInside)


#3

Не работает. Краш уходит, но функция не срабатывает


#4

Отлично все работает

RexHunt Показал на ошибку

Screen


#5
Офтоп

Странный стиль Xcode немного :slight_smile: