以编程方式快速解除警报

时间:2016-06-09 18:37:49

标签: swift swift2

我需要UIAlert的一点帮助。我收到错误"UIAlertController can only have one action with a style of UIAlertActionStyleCancel"。我知道这是因为我在函数之外启动此警报,但不确定如何修复它。如何在if blah < 80 {条件中访问警报?

let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
@IBAction func blahButtonPressed(sender: AnyObject) {        

        alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
            // I have code here
        }))

        alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
            // I have code here
        }))

        alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
            // I have code here
        }))

        presentViewController(alertView, animated: true, completion: nil)

}

稍后在代码中我通过蓝牙获取值,如果值低于80,则需要关闭警报。

if blah < 80 {
alertView.dismissViewControllerAnimated(true, completion: nil)
}

2 个答案:

答案 0 :(得分:2)

我不是百分之百,但只按一下按钮就可以了吗?如果是,那么可能是因为您要将操作添加到0 1 0 1 1 / 'STACK' // Substring is now 'TCK' 内的alertView。相反,您可能想尝试在@IBAction之外移动UIAlertAction的添加,并仅在其中显示警报视图。像这样:     

@IBAction

这样每次按下“blahButton”时都不会添加let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert) alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in // I have code here })) alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in // I have code here })) alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in // I have code here })) @IBAction func blahButtonPressed(sender: AnyObject) { presentViewController(alertView, animated: true, completion: nil) } (这将导致多个UIAlertAction的样式为“UIAlertActionStyleCancel”)

答案 1 :(得分:0)

如果有人遇到这种情况,我就会修复它。

 public void showTruitonDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

    public static class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
            DateEdit1.setText(DateEdit1.getText() + " " + day + "/" + (month + 1) + "/" + year);
        }
    }

//当我需要关闭时如果blah低于80

var newAlert: AnyObject?


@IBAction func blahButtonPressed(sender: AnyObject) {        
    let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

    alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

    alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

   newAlert = alertView

        presentViewController(alertView, animated: true, completion: nil) 
}