How to use an UIActionSheet within an if statement

2020-05-10 09:21发布

问题:

I have an if statement that is working fine, but I need to add a 2nd if statement inside of it and I can't seem to figure out how to get it right.

Here is my code:

-(IBAction)xButton {
    if([_hasUserTakenAPhoto  isEqual: @"YES"]) {
        _xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:@"Delete" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
        [_xButtonAfterPhotoTaken showInView:self.view];
        NSString *title = [_xButtonAfterPhotoTaken buttonTitleAtIndex:1];

        if(title isEqualToString:@"Delete") {
            [self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
        }
    } else {
        [self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
    }
}

I get errors when I add in the 2nd if statement of:

if(title isEqualToString:@"Delete") {
    [self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
}

I have tried making the 2nd if statement an "else if" but then it will not let me access the NSString object called "title". Is there an easier way to do this, or should I just make title a global variable?

回答1:

UIActionSheets are not used that way:

- (IBAction)xButton:(UIButton*)sender
{
    if ([_hasUserTakenAPhoto isEqual:@"YES"])
    {
        _xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:@"Delete Photo?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:nil];
        [_xButtonAfterPhotoTaken showInView:self.view];
    } 
    else
    {
        [self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Check if it's the correct action sheet and the delete button (the only one) has been selected.
    if (actionSheet == _xButtonAfterPhotoTaken && buttonIndex == 0)
    {
        [self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
    }
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
    NSLog(@"Canceled");
}

You gotta understand that interface elements are not "instant", there's plenty of asynchrony going on. When presenting an UIActionSheet for instance, the thread doesn't wait for the user to answer yes or no, it keeps running.

That's why there's delegates, and blocks, you present the UIActionSheet, and with the delegate you say "I'll take care of it when the user actually clicks it".

You'd be wondering, why not just wait for it to select it? Main thread takes care of updating the interface, animations, and retrieving user input (touches, keyboard taps, etc) and even running NSTimers that are subscript to the main NSRunLoop. Stopping main thread would lock the interface.



回答2:

Try

- (IBAction)xButton
{
    NSString *title;

    if ([_hasUserTakenAPhoto isEqual:@"YES"])
    {
        _xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:@"Delete" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];

        [_xButtonAfterPhotoTaken showInView:self.view];

        title = [_xButtonAfterPhotoTaken buttonTitleAtIndex:1];

        if ([title isEqualToString:@"Delete"])
        {
            [self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
        }
    }
    else
    {
        [self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
    }
}