Как мне прикрепить AlertView к BarButton в Swift? - PullRequest
0 голосов
/ 07 марта 2020

У меня есть этот фрагмент кода в моем viewDidLoad, который я хотел бы присоединить к BarButton, однако, когда я помещаю его в функцию BarButton, не работает. Как мне его разместить?

let defaultAction = UIAlertAction(title: "Continue",
                             style: .default) { (action) in

        }
        let cancelAction = UIAlertAction(title: "Cancel",
                             style: .cancel) { (action) in

        }


        let alert = UIAlertController(title: "Wait!",
              message: "If you go back, you will lose all progress. Are you sure?",
              preferredStyle: .alert)
        alert.addAction(defaultAction)
        alert.addAction(cancelAction)

        self.present(alert, animated: true) {

        }

Ответы [ 2 ]

0 голосов
/ 07 марта 2020

Если вы предполагаете прикрепить кнопку UIB справа или слева, следуйте приведенному ниже коду

    let rightButtonItem = UIBarButtonItem(image: //your Image,
                                          style: .plain,
                                          target: self,
                                          action: #selector(searchButtonAction(sender:)))

    rightButtonItem.tintColor = .black // your desired colour
    self.navigationItem.rightBarButtonItem = rightButtonItem // if it is Left side then leftBarButtonItem

, тогда действие должно быть похоже на

@objc
func searchButtonAction(sender: UIBarButtonItem) {
    let alert = UIAlertController(title: // Your title,
                                  message:// Your message,
                                  preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: // Your button Title, style: .default) { _ in
        // Your action
    })

    alert.addAction(UIAlertAction(title: // Your button Title, style: .cancel) {  _ in
        // Your action
    }) 
    alert.view.tintColor = UIColor(asset: Asset.Colors.black)// Your desired Colour
    self.present(alert, animated: true)
}
0 голосов
/ 07 марта 2020

все, что вам нужно, чтобы создать функцию, а затем вызвать эту функцию в действии BarButton, чтобы оно работало. Спасибо ..

@IBAction func bar_button_Action(_ sender: Any) {
    self.BarButtonshowalert(message: "Your message like: - If you go back, you will lose all progress. Are you sure?")
}     

   func BarButtonshowalert(message:NSString){  //create function for showealert and then we will call in this function into barbutton IBAction so it will work's

    let defaultAction = UIAlertAction(title: "Continue", style: .default) { (action) in

    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in

    }


    let alert = UIAlertController(title: "Wait!", message: message, preferredStyle: .alert)
    alert.addAction(defaultAction)
    alert.addAction(cancelAction)

    self.present(alert, animated: true)
   }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...