【问题标题】:How to popup AlertView on successful check of pin in ObjC?如何在 ObjC 中成功检查引脚时弹出 AlertView?
【发布时间】:2018-10-10 11:15:48
【问题描述】:

我正在尝试创建一个小应用程序,其中用户需要在正确的引脚上输入引脚,它会说正确的引脚,否则错误,但我不确定我的逻辑是否正确。我在最新的Mac 版本上使用Xcode10。

- (IBAction)validatePin:(id)sender {
[ViewController checkPin:self.textPin.text.integerValue];
}

+(BOOL)checkPin:(NSInteger)pin {
if (pin == 1408)
{
   //[UIAlertController alertControllerWithTitle:@"Pin" message:@"Success" preferredStyle:UIAlertControllerStyleAlert];
    [[UIAlertView alloc] initWithTitle:@"Alert Title"
                               message:@"are you sure?"
                              delegate:self
                     cancelButtonTitle:@"No"
                     otherButtonTitles:@"Yes", nil];
    return YES;
}
[UIAlertController alertControllerWithTitle:@"Pin" message:@"Fail" preferredStyle:UIAlertControllerStyleAlert];
return NO;}

我已经尝试了UIAlertView 和UIAlertController 方法,但我没有得到对应用程序的响应。有人可以在ObjC 中更正此代码吗?

谢谢

【问题讨论】:

    标签: ios objective-c xcode10


    【解决方案1】:

    不需要创建方法,可以直接这样查看并设置alert。

    - (IBAction)validatePin:(id)sender {
        if (self.textPin.text.integerValue == 1408) {
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert Title"
                                                                       message:@"PIN is Correct"
                                                                preferredStyle:UIAlertControllerStyleAlert];
    
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
    
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        else {
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert Title"
                                                                       message:@"PIN is WRONG"
                                                                preferredStyle:UIAlertControllerStyleAlert];
    
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
    

    如果您想使用您的代码,请将其更改为此。

    - (IBAction)validatePin:(id)sender {
        BOOL isValidPin = [ViewController checkPin:self.textPin.text.integerValue];
    
        if (isValidPin) {
            // SHOW RIGHT ALERT
        }
        else {
            // SHOW WRONG ALERT
        }
    }
    
    +(BOOL)checkPin:(NSInteger)pin {
        if (pin == 1408) {
            return YES;
        }
        else {
            return NO;
        }
    }
    

    【讨论】:

    • @user2670674,请接受这个答案,当你有足够的声誉时,请投票。
    猜你喜欢
    • 2011-09-04
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 2012-09-10
    • 1970-01-01
    • 2014-01-07
    • 2020-12-03
    相关资源
    最近更新 更多