单击按钮时的UIAlertController

时间:2016-09-02 06:38:46

标签: swift uialertcontroller

打开UIAlertController按钮单击,该操作将打开,但主要问题是未对其单击执行UIAlertAction方法。这是代码块:

  class HomeViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

            // getData()
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

//menuBtn is the button

    @IBAction func menuBtn(sender: UIButton) {
          let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

        let orders = UIAlertAction(title: "Orders", style: .Default, handler: { (alert: UIAlertAction!) -> Void in
            let alertViewController = self.storyboard?.instantiateViewControllerWithIdentifier("OrdersViewController") as! OrdersViewController
            self.presentViewController(alertViewController, animated: true, completion: nil)
        })


        let about = UIAlertAction(title: "About", style: .Default, handler: {(alert: UIAlertAction!) -> Void in
            let aboutObject = self.storyboard?.instantiateViewControllerWithIdentifier("AboutViewController") as! AboutViewController
            self.presentViewController(aboutObject, animated: true, completion: nil)

            })

        let contactUs = UIAlertAction(title: "Contact Us", style: .Default, handler: {(alert: UIAlertAction!) -> Void in

            let alertViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContactViewController") as! ContactViewController
            self.presentViewController(alertViewController, animated: true, completion: nil)
        })

        let login = UIAlertAction(title: "LogIn", style: .Default, handler: {(alert: UIAlertAction!) -> Void in
            let alertViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
            self.presentViewController(alertViewController, animated: true, completion: nil)
        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)


        optionMenu.addAction(orders)
        optionMenu.addAction(about)
        optionMenu.addAction(contactUs)
        optionMenu.addAction(login)
        optionMenu.addAction(cancelAction)

        self.presentViewController(optionMenu, animated: true, completion: nil)
    }

3 个答案:

答案 0 :(得分:0)

在按钮上单击操作,您必须编写代码..尝试此代码。

 let alert = UIAlertController(title: "Saved", message: "Selected Frame is Saved", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Ok", style:.Default , handler: { (UIAlertAction) in

        }))
//Add action like this


        self.presentViewController(alert, animated: true, completion: nil)

仍然需要任何帮助随时问。

答案 1 :(得分:0)

首先检查按钮动作是否在内部触摸。使按钮动作在内部修饰。下面的代码适合我。希望这也适合你。根据需要更改动作标题。

 @IBAction func menuBtn(sender: AnyObject) {
    let actionSheet = UIAlertController()
    let criticalAction = UIAlertAction(title : "CRITICAL" , style : UIAlertActionStyle.Default){
        (action) in
       //This section will be executed when the buttons are pressed
        //Do your work here.
        debugPrint("CRITICAL")
    }

    let highAction = UIAlertAction(title : "HIGH" , style : UIAlertActionStyle.Default){
        (action) in
      //This section will be executed when the buttons are pressed
        //Do your work here.
      debugPrint("HIGH")
   }

    actionSheet.addAction(criticalAction)
    actionSheet.addAction(highAction)

    self.presentViewController(actionSheet, animated: true, completion: nil)        
}

答案 2 :(得分:0)

这是代码工作正常,我已经检查过它还打开了新的viewController

交叉检查点:

  1. 控制器类和stroybaord已连接
  2. Storyboard ID已被分配
  3. IBAction必须连接到IBOutlet