gpt4 book ai didi

swift - 在 UIAlertViewDelegate 中的索引处单击的按钮未被调用

转载 作者:可可西里 更新时间:2023-11-01 01:06:54 24 4
gpt4 key购买 nike

在早期的 Objective-C 中,我们在 UIAlertView 中使用了 delegate:self,这样我们就可以在索引处调用类似 buttonclicked 的方法....但现在在 swift 中我应该在哪里设置该委托(delegate)?因为我的 buttonclicked at index 方法没有被调用..

import UIKit

class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate{

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
let cell :UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: nil)
cell.text = "HI \(indexPath.row)"
cell.detailTextLabel.text = "SWIFT"
return cell
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return 5
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{


var alert = UIAlertController(title: "Demo", message: "You have selected row number\(indexPath.row)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "KILL", style: UIAlertActionStyle.Destructive, handler: nil))
alert.addAction(UIAlertAction(title: "PUNE", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Mumbai", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Kolkata", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Chennai", style: UIAlertActionStyle.Default, handler: nil))

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


}
func demoFunction (myName:NSString)
{
NSLog("%@", "swapnil")
}

func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
{
NSLog("%@", "YOU PRESSED OK" )
switch (buttonIndex)
{
case 0:NSLog("%@", "YOU PRESSED OK" )
case 1:NSLog("%@", "YOU PRESSED KILLER" )
case 2:NSLog("%@", "YOU PRESSED PUNE" )
case 3:NSLog("%@", "YOU PRESSED MUMBAI" )
case 4:NSLog("%@", "YOU PRESSED KOLKATA" )
case 5:NSLog("%@", "YOU PRESSED Chennai" )
default :NSLog("%@", "You have pressed default")

}

}

}

最佳答案

你不应该使用委托(delegate)方法,事实上 UIAlertController 不提供委托(delegate)。

在您的情况下,您的警报操作应该是这样的:

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:    {(alert :UIAlertAction!) in
println("YOU PRESSED OK")

}))

等等每一个 Action !

关于swift - 在 UIAlertViewDelegate 中的索引处单击的按钮未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24037612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com