简体   繁体   English

如何使用swift代码在按钮单击中在自定义UITableViewCell中显示UIAlertController?

[英]how to show UIAlertController in a custom UITableViewCell in button click using swift code?

i want to show UIAlertController in a custom UITableViewCell in a button click. 我想在按钮单击中在自定义UITableViewCell中显示UIAlertController。 but i can't find any way for this. 但我找不到任何办法。 please see the below code that i have try. 请看下面我试过的代码。 在此输入图像描述

this code give error in UITableViewCel, that is shown in above picture. 此代码在UITableViewCel中给出错误,如上图所示。 please help me.the whole class code is below: 请帮帮我。全班代码如下:

class CustomCellDeerCalls: UITableViewCell {

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var detialsInfoButton: UIButton!

@IBOutlet weak var DeerCallNameLabel: UILabel!

var arrayOfDetialsInfoDeerCalls: [String]=["This sound is a doe's way of locating other deer of her family group . This call can be used all year long.",
"Grunts are a doe's way of saying come here , also to call her fawns at feeding time . It is critical to keep the call soft as a loud grunt is too aggressive of a call.",
"A buck grunt is a deeper pitch than a doe grunt , it means the same thing . The older the buck the deeper the tone. ",
"As the buck chases the doe as the rut approaches he is frustrated, and makes a series of soft grunts while trailing her. It's the bucks way of asking her to stop so he can be breed with her.",
"This sound's a lot like a calf bawl , but it is a series of buck bleats. This signals the bucks desire for company.",
"This is a non aggressive and social behaviour that all bucks do after shedding their velvet . This is when the bucks learn who can whip the other . This process does not prevent serious fights later on during the rut.",
"This is the sound that a buck makes during the courtship when the doe stops running , but won't let the buck breed her. It's a non aggressive and frustration call by him.",
"This is the sound that a Doe makes to signal that her breeding time is near.",
"This is the sound that a Doe makes to signal that she ready to breed RIGHT NOW.",
    "Deer make this sound to intimidate other deer and prevent fights. This call is often made by a rut- crazed buck when confronted with a rival. This sound can send smaller buck running from the area.",
"Another rut crazed Bucks sound to intimidate other deer and prevent fights when confronted with a rival. This sound can also send smaller buck running from the area.",
"This is a short aggressive rattling sequence to possibly lure in less aggressive, but curious buck, as well as the local dominant whitetail buck of the area. To make this sound like a real fight sniffs, wheezes and grunts have also been thrown in for added effect."]


var arrayOfDeerSoundsCall: [String]=["CONTACT","doegrunt","bkgrunt","tend","bawl","spar","rage","estrusb","bellow","sniff","wheeze","rattle"]

// var arrayOfCallsName: [String]=[""] // var arrayOfCallsName:[String] = [“”]

@IBAction func clickDetialsInfoButton(sender: AnyObject) {

   var alert = UIAlertController(title: "dddddd", message: arrayOfDetialsInfoDeerCalls[sender.tag], preferredStyle: UIAlertControllerStyle.Alert)
   alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

self.presentViewController(alert, animated: true, completion: nil)
    //sender.sup
   //
}

var audioPlayer = AVAudioPlayer()


@IBAction func clickplayButton(sender: AnyObject) {

    println(sender.tag)

    var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(arrayOfDeerSoundsCall[sender.tag], ofType: "wav")!)
    println(alertSound)

    // Removed deprecated use of AVAudioSessionDelegate protocol
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    AVAudioSession.sharedInstance().setActive(true, error: nil)

    var error:NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
    audioPlayer.prepareToPlay()
    audioPlayer.play()



}
override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
func setCell(callName: String){

self.DeerCallNameLabel.text=callName

}

} }

i found a solution. 我找到了解决方案。 following are code for above question. 以下是上述问题的代码。 write the following code for button in viewcontroller where table view are present. 在viewcontroller中为按钮编写以下代码,其中存在表视图。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let deerNameCell: CustomCellDeerCalls = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCellDeerCalls
    let deercallcell=arrayOfCallsName[indexPath.row]

    deerNameCell.DeerCallNameLabel.text=deercallcell.callName
    deerNameCell.playButton.tag=indexPath.row
    deerNameCell.detialsInfoButton.tag=indexPath.row
    deerNameCell.detialsInfoButton.addTarget(self, action: "showAlert:", forControlEvents:UIControlEvents.TouchUpInside)

    return deerNameCell

}

and the funtion for alert in the same view controller: 和相同视图控制器中的警报功能:

   func showAlert(sender:UIButton!)
{
    println(sender.tag)
    let deercallcell=arrayOfCallsName[sender.tag]
    var alert = UIAlertController(title: deercallcell.callName, message: arrayOfDetialsInfoDeerCalls[sender.tag], preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

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

}

You are trying to call presentViewController on a UITableViewCell but this method is a member of a UIViewController instead. 您正尝试在UITableViewCell上调用presentViewController ,但此方法是UIViewController的成员。

Here is my suggestion: 这是我的建议:

1) Create a CustomCellDeerCallsDelegate protocol, ie.: 1)创建CustomCellDeerCallsDelegate协议,即:

protocol CustomCellDeerCallsDelegate {
    func showAlert(title:String, message:String);
}

2) Add a weak property to your custom cell: 2)向自定义单元格添加弱属性:

var delegate:CustomCellDeerCallsDelegate?

3) In your clickDetialsInfoButton function call a method on a delegate: 3)在clickDetialsInfoButton函数中调用委托上的方法:

self.delegate?.showAlert("DDDDD", message: arrayOfDetialsInfoDeerCalls[sender.tag])

4) Add a protocol implementation to a ViewController that hosts the table view that displays the cell. 4)将协议实现添加到ViewController,该ViewController承载显示单元格的表视图。

5) In a protocol implementation of showAlert function - show the alert: 5)在showAlert函数的协议实现中 - 显示警报:

func showAlert(title:String, message:String){
    var alert = UIAlertController(title: "dddddd", message: arrayOfDetialsInfoDeerCalls[sender.tag], preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM