iphone UIAlertView Modal

后端 未结 5 861
小蘑菇
小蘑菇 2020-12-10 14:50

Is it possible to present a UIAlertView and not continue executing the rest of the code in that method until the user responds to the alert?

Thanks in advance.

5条回答
  •  时光取名叫无心
    2020-12-10 15:37

    I just came across this question for MonoTouch, and trying to find a previous answer ran into this open question.

    Yes, it is possible. The following sample in C# shows how you can do it with MonoTouch, an Objective-C version of this should be easy to write:

    To do this, what you can do is to run the mainloop manually. I have not managed to stop the mainloop directly, so I instead run the mainloop for 0.5 seconds and wait until the user responds.

    The following function shows how you could implement a modal query with the above approach:

    int WaitForClick ()
    {
        int clicked = -1;
        var x = new UIAlertView ("Title", "Message",  null, "Cancel", "OK", "Perhaps");
        x.Show ();
        bool done = false;
        x.Clicked += (sender, buttonArgs) => {
            Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex);
        clicked = buttonArgs.ButtonIndex;
        };    
        while (clicked == -1){
            NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5));
            Console.WriteLine ("Waiting for another 0.5 seconds");
        }
    
        Console.WriteLine ("The user clicked {0}", clicked);
        return clicked;
    }
    

提交回复
热议问题