iphone UIAlertView Modal

大城市里の小女人 提交于 2019-11-28 11:07:06

I guess stopping the code you meant was to stop the device to run the next code you have written after the alertview

For that just remove your code after your alertview and put that code in the alertview delegate

-(void) yourFunction
{
     //Some code
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Your Message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
     //Remove all your code from here put it in the delegate of alertview
}
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:    (NSInteger)buttonIndex 
{
if(buttonIndex==0)
    {
        //Code that will run after you press ok button 
    }
}

Dont forget to include UIAlertViewDelegate in the .h file

An answer has already been accepted, but I'll note for anyone who comes across this question that, while you shouldn't use this for normal alert handling, in certain circumstances you may want to prevent the current execution path from continuing while an alert is being presented. To do so, you can spin in the run loop for the main thread.

I use this approach to handle fatal errors which I want to present to the user before crashing. In such a case, something catastrophic has happened, so I don't want to return from the method that caused the error which might allow other code to be executed with an invalid state and, for example, corrupt data.

Note that this won't prevent events from being processed or block other threads from running, but since we're presenting an alert which essentially takes over the interface, events should generally be limited to that alert.

// Present a message to the user and crash
-(void)crashNicely {

  // create an alert
  UIAlertView *alert = ...;

  // become the alert delegate
  alert.delegate = self;

  // display your alert first
  [alert show];

  // spin in the run loop forever, your alert delegate will still be invoked
  while(TRUE) [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];

  // this line will never be reached
  NSLog(@"Don't run me, and don't return.");

}

// Alert view delegate
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
  abort(); // crash here
}

No, but the easy fix is to split your code at the point you present the UIAlertView - and start the second part from your delegate method when the alert is dismissed.

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;
}

If the code that needs to wait is all in the same method (or can be), blocks can be another option. I've created a UIAlertView subclass to handle this. Just keep in mind that execution will still continue past the [alert show] call; this just gives you a way to pass along your stack variables without making new in-between class variables. Also, if you aren't already familiar with Objective-C blocks, you should read the documentation on it; there are a few gotchas and some odd syntax you should be aware of.

Goes something like this:

typedef void (^MyAlertResult)(NSInteger clickedButtonIndex);

@interface MyBlockAlert : UIAlertView
{
    MyAlertResult finishedBlock;
}
- (void) showWithCompletionBlock:(MyAlertResult)block;
...

- (void) showWithCompletionBlock:(MyAlertResult)block
{
    self.delegate = self;
    finishedBlock = [block copy];
    [self show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    finishedBlock(buttonIndex);
    [finishedBlock release];
    finishedBlock = nil;
}

used like this:

__block NSArray* arrayOfDeletes; // set to something
MyBlockAlert* areYouSureAlert = [[MyBlockAlert alloc] initWithTitle:@"Really delete?" message:@"Message" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];

[arrayOfDeletes retain]; // Make sure retain count is +1

[areYouSureAlert showWithCompletionBlock:
^(NSInteger clickedButtonIndex)
{
    if (clickedButtonIndex == 1)
    {
        // Clicked okay, perform delete
    }
    [arrayOfDeletes release];
}];
[areYouSureAlert release];

This was a quick implementation, so feel free to find any bugs; but you get the idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!