7

私は初心者レベルのプログラマーです。アラートのボタンにアクションを追加しようとしていますが、機能しません。アラート ボタンを選択してラベルのテキストを変更できるかどうかをテストしたいだけですが、機能しません。もちろん、アラートとボタンはよく見えますが、ボタンをクリックしても何も起こりません。

@IBOutlet var statusLabelAlert : UILabel

var alertTest = UIAlertView()

@IBAction func alertButton(sender : AnyObject) {

    alertTest.message = "Select one!"
    alertTest.addButtonWithTitle("1st")
    alertTest.addButtonWithTitle("2nd")
    alertTest.addButtonWithTitle("3rd")
    alertTest.title = "Test Alert"
    alertTest.show()

}
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){
    switch buttonIndex{
    case 0:
        statusLabelAlert.text = "1st"
    case 1:
        statusLabelAlert.text = "2nd"
    case 2:
        statusLabelAlert.text = "3rd"
    default:
        statusLabelAlert.text = "error"
    }

}
4

5 に答える 5

3
@IBOutlet var statusLabelAlert : UILabel
var alertTest = UIAlertView()
@IBAction func alertButton(sender : AnyObject)
{
    alertTest.delegate = self
    alertTest.message = "Select one!"
    alertTest.addButtonWithTitle("1st")
    alertTest.addButtonWithTitle("2nd")
    alertTest.addButtonWithTitle("3rd")
    alertTest.title = "Test Alert"
    alertTest.show()

}

func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
{
    switch buttonIndex
    {
        case 0:
        statusLabelAlert.text = "1st"
        case 1:
        statusLabelAlert.text = "2nd"
        case 2:
        statusLabelAlert.text = "3rd"
        default:
        statusLabelAlert.text = "error"
   }
}
于 2014-07-05T04:54:41.660 に答える
2
IBOutlet var statusLabelAlert : UILabel

@IBAction func alertButton(sender : AnyObject) {

let alert = UIAlertController(title: "Alert", message: "This is an Alert", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "1st", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
statusLabelAlert.text = "1st" 
}))

alert.addAction(UIAlertAction(title: "2nd", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
statusLabelAlert.text = "2nd" 
}))

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

}

ボタンが押されたときに sth を実行したくない場合:

alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
于 2015-10-13T14:36:55.053 に答える
1

これが iOS 8 の場合は、UIAlertController と UIAlertAction に切り替える必要があります。UIAlertAction には、ボタンの動作が含まれています。

于 2014-07-04T23:52:45.480 に答える