如何在if语句中使用UIActionSheet? [英] How to use an UIActionSheet within an if statement?

查看:76
本文介绍了如何在if语句中使用UIActionSheet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如果语句,工作正常,但我需要添加第二个 if

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.

这是我的代码:

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

of:

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

我试过使第二个if语句else if不会让我访问名为title的NSString对象。

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?

推荐答案

是否有更简单的方法来做这个, UIActionSheet 不是这样使用的:

- (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");
}

你必须明白界面元素不是即时,有很多不同步继续。例如,当呈现 UIActionSheet 时,该线程不等待用户回答是或否,它仍在运行。

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.

这就是为什么有代理和块,你提出 UIActionSheet ,并与代理人说,我会照顾它,当用户实际点击它。

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".

你会想,为什么不等到它选择呢?主线程负责更新接口,动画和检索用户输入(触摸,键盘敲击等),甚至运行 NSTimers ,它们是< c> NSRunLoop 。停止主线程将锁定接口。

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.

这篇关于如何在if语句中使用UIActionSheet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆