4

I'm using a very basic UIAlertController setup with two buttons.

    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)

This code is working on the same View with a push segue from a different part of the program. Where the structure is:

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

Now the AlertController popups and the button return the simple text output.

But in the following situation the AlertController popups but clicking button returns nothing, my breakpoint never fires or just checking the console for the text output returns nothing.

(MainView) -modal> (ThisView)

Of Course I tried setting the segue in the second situation to a push segue, but it didn't change anything. So to make no mistake, the AlertController does popup, but the buttons don't respond at all. Can I make a alternative delegate?

Now it just costed me a full day of getting something simple to work. I've tried most tricks like delays or a async block.

Now I'm kind of out of things to try. So please someone enlighten me what I'm doing wrong :)

UPDATE

Embedding the MainView in a navigation controller and changing the segue to a Push segue, makes the button presses respond.. Adding a navigation controller to fix this is something I could consider, but I would like to keep the Modal segue for its animation. Any Ideas?

Temporary solution

For now I'll use the push segue and the navigation controller to solve this behaviour. It is not optimal, ,since this view should behave as a popup and not a push like segue, but it works.

Henk-Martijn
  • 2,024
  • 21
  • 25
  • Could you specify where the code is declared? In the MainView or in ThisView? – aout May 11 '15 at 10:02
  • Can you post some more code/storyboard to give us a better understanding of what's going on when this alert is being shown? – keithbhunter May 11 '15 at 11:40
  • The code is declared on ThisView, which is just 1 segue away from the mainView. ThisView can also be reached from a different part of the program, which has a x amount of push segues in between. On ThisView, there is also a timer ticking every second, but I disabled that and most things happening there so it should not do anything. Also I runned the Alert from different parts of the program, viewdidload, viewdidappear and in one of the loops (which are disabled when the timer if disabled, might you ask to disable that) So my thought has todo with the modal segue construction.. – Henk-Martijn May 11 '15 at 12:42
  • are you dismissing `modal` view controller when you presenting alert? – Silmaril May 14 '15 at 22:26
  • No, but that would be the function when pressing the leave button. But those buttons does not get registered on top of the modal View. When I change it to a push (segue) and connect it to a navigation controller, the button do respond.. – Henk-Martijn May 15 '15 at 09:47
  • Weird, because the only possible way I can think of when you'll get such behaviour if `UIAlertController` is deallocated. That means that `self` view controller in `self.presentViewController` should be deallocated as well. But you are saying that you don't dismissing it... – Silmaril May 15 '15 at 10:47
  • Use the debugger to list current controller hierarchy. – Sulthan May 16 '15 at 22:23
  • have you tried setting the [`ModalPresentationStyle`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/c/tdef/UIModalPresentationStyle)? – luk2302 May 17 '15 at 12:44
  • when you are presenting alert controller? I believe its because of Model Presentation of view controller – Syed Ali Salman May 17 '15 at 16:38
  • Can you upload a sample project? – Léo Natan May 17 '15 at 16:57

2 Answers2

1

I can't tell much from the code you have listed, but I have come across similar issues before, and the cause was that something was deallocating before I expected it to, and thus never received its message.

The best solution I've found is to enable NSZombies. Xcode will tell you when your action message is getting sent to a deallocated instance. See this answer for how to enable (use the Xcode 4.1 or greater approach): https://stackoverflow.com/a/5387006/2194039

If you don't want to use NSZombies, try putting this into each of your VCs, or other relevant classes:

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

I know it's just a general idea and nothing concrete, but this has helped me before.

Community
  • 1
  • 1
justinpawela
  • 1,968
  • 1
  • 14
  • 18
  • Thank you for your suggestion, but it did not help me. For the moment I leave the whole functionality aside, until there is a solution without having to use push segues. – Henk-Martijn May 17 '15 at 12:33
  • I will not downvote, because the thinking is good, but once a controller is presented or pushed, it is held strongly (retain) by the system, so it would not release the alert controller or the other ones. – Léo Natan May 17 '15 at 16:56
0

have you tried setting the action in a separate function , and call that function

like this

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)

and then define the methods like this

func AlertContinue() {println("here")}
func AlertLeave() {println("here")}
  • Thank you for your reply, but the behaviour did not change. It is like the click events are not getting registered at all.. – Henk-Martijn May 13 '15 at 08:40
  • one more thing that comes to mind , from the story board , pick (ThisView) and go to attribute inspector , and check if Presentation is set to full screen . also , check (MainView) attributes , for the (Defines Context) checkmark , uncheck it and try – Mos'ab Abo Marzook May 13 '15 at 08:58
  • Both are set like you say. I updated my problem description with a possible fix, but it is not what I would like to use if possible :) – Henk-Martijn May 13 '15 at 09:06
  • If the handler blocks are not called, how exactly will this help? Posting for posting sake does not help. – Léo Natan May 17 '15 at 16:55