UIAlertController按钮代表不要开火(在Modally Segue中)

时间:2015-05-06 15:49:33

标签: ios swift uialertcontroller

我使用带有两个按钮的非常基本的UIAlertController设置。

    var alert = UIAlertController(title: "Leave player?",
        message: "You will not be able to return to this routine after!",
        preferredStyle: .Alert)


    let cancelAction: UIAlertAction = UIAlertAction(title: "Continue", style: .Cancel) { action -> Void in
        println("here")
    }
    let deleteAction: UIAlertAction = UIAlertAction(title: "Leave", style: .Default) { action -> Void in
        println("here")
    }

    alert.addAction(cancelAction)
    alert.addAction(deleteAction)

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

此代码使用来自程序不同部分的push segue处理同一个View。结构是:

(MainView) -push> (View1) -push> (View2) -push> (ThisView)

现在AlertController弹出,按钮返回简单的文本输出。

但是在下面的情况下,AlertController会弹出,但是click按钮什么都不返回,我的断点永远不会触发,或只是检查控制台的文本输出什么都不返回。

(MainView) -modal> (ThisView)

当然我尝试将第二种情况下的segue设置为推送segue,但它并没有改变任何东西。 所以毫无疑问,AlertController会弹出,但按钮根本没有响应。我可以成为替代代表吗?

现在它花了我一整天的时间来完成简单的工作。我尝试了大多数技巧,比如延迟或异步阻止。

现在我尝试了一些事情。所以请有人告诉我我做错了什么:)

更新

将MainView嵌入导航控制器将segue更改为Push segue,使按钮按下响应.. 我可以考虑添加一个导航控制器来解决这个问题,但我想保留Modal segue的动画效果。 任何想法?

临时解决方案

现在我将使用push segue和导航控制器来解决此问题。它不是最佳的,因为这个视图应该像弹出窗口一样,而不是像segue一样推动,但它可以工作。

2 个答案:

答案 0 :(得分:1)

我从你列出的代码中说不出多少,但我之前遇到过类似的问题,原因是在我预期之前已经解除了一些东西,因此从未收到过它的消息。

我发现的最佳解决方案是启用NSZombies。 Xcode会告诉您何时将您的操作消息发送到解除分配的实例。请参阅此答案以了解如何启用(使用Xcode 4.1或更高版本的方法): https://stackoverflow.com/a/5387006/2194039

如果您不想使用NSZombies,请尝试将其放入您的每个VC或其他相关类中:

deinit {
    println("This instance is deallocating")
}

我知道这只是一个普遍的想法而且没有什么具体的,但这对我有所帮助。

答案 1 :(得分:0)

您是否尝试在单独的函数中设置操作,并调用该函数

像这样

var alert = UIAlertController(title: "Leave player?", message: "You will not be able to return to this routine after!", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Cancel, handler: {(alert : UIAlertAction!) in self.AlertContinue()}))

alert.addAction(UIAlertAction(title: "Leave", style: UIAlertActionStyle.Default, handler: {(alert : UIAlertAction!) in self.AlertLeave()}))

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

然后定义像这样的方法

func AlertContinue() {println("here")}
func AlertLeave() {println("here")}