【问题标题】:Objective-c functionObjective-c 函数
【发布时间】:2014-11-07 09:48:39
【问题描述】:

我正在尝试创建一个函数来保存一些代码,但是我不知道该怎么做。

if (count > 100 && count < 120)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!"
                                                         message:[NSString stringWithFormat:@"You're really good at this! You scored %i points", count - 1]
                                                        delegate:self
                                               cancelButtonTitle:@"Play Again"
                                               otherButtonTitles:nil];
        [alert show];
    }

    else if(count > 120)
    {
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Time is up!"
                                                         message:[NSString stringWithFormat:@"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1]
                                                        delegate:self
                                               cancelButtonTitle:@"Play Again"
                                               otherButtonTitles:nil];
        [alert2 show];
    }

    else
    {
        UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Time is up!"
                                                        message:[NSString stringWithFormat:@"Not very good, you scored %i points", count - 1]
                                                       delegate:self
                                              cancelButtonTitle:@"Play Again"
                                              otherButtonTitles:nil];
        [alert3 show];
    }

如您所见,代码非常重复,我想创建一个函数来更改“alert”变量和消息字符串的值。因此,当我调用函数时,我只输入函数名称和警报变量应该具有的值,在这种情况下; 1、2 和 3 以及消息文本取决于我们正在查看的条件。

那么有什么方法可以做到这一点吗?必须有,因为像这样重复的代码永远不会好看!如果有人可以帮助我,我将不胜感激,我尝试查看一些参考资料,但没有一个能让我更接近解决问题。

【问题讨论】:

  • 如果count == 120,你就没有很好地处理这种情况。你应该做这样的事情:if (count &lt; 100) { ... } else if (count &lt; 120) { ... } else { ... },这将是处理三个分支的一种不那么模糊的方式。

标签: objective-c xcode function ios7


【解决方案1】:

只需查看重复的功能并检查不同的值。在这种情况下,它只是消息:

NSString *message = nil;
if (count > 100 && count < 120)
{
    message = [NSString stringWithFormat:@"You're really good at this! You scored %i points", count - 1];
}
else if(count >= 120)
{
    message = [NSString stringWithFormat:@"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1];
}
else
{
    message = [NSString stringWithFormat:@"Not very good, you scored %i points", count - 1];
}

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!"
                                                message:message
                                               delegate:self
                                      cancelButtonTitle:@"Play Again"
                                      otherButtonTitles:nil];
[alert show];

(编辑:您错过了会从裂缝中溜走的值120)。

【讨论】:

  • 谢谢,这两种解决方案都很好用。特洛伊木马的解决方案是最好的。我很愚蠢地认为我必须更改变量名称。
【解决方案2】:

我正在创建一个新函数,它使用给定的消息创建一个新警报:

-(void)showAlertWithMsg:(NSString *)msg tag:(NSInteger)tag
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!"
                                                message:msg
                                               delegate:self
                                      cancelButtonTitle:@"Play Again"
                                      otherButtonTitles:nil];
alert.tag = tag;
[alert show];

}

要调用此函数,请编写以下代码:

 if (count > 100 && count < 120)
{
[self showAlertWithMsg:[NSString stringWithFormat:@"You're really good at this! You scored %i points", count - 1] tag:1];
}
else if(count > 120)
{
    [self showAlertWithMsg:[NSString stringWithFormat:@"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1] tag:2];
}
else
{
    [self showAlertWithMsg:[NSString stringWithFormat:@"Not very good, you scored %i points", count - 1] tag:3];

}

还有一件事我也传递了标签。所以如果你想为不同的警报执行不同的操作。

【讨论】:

    【解决方案3】:

    您可以为现有的 UIAlertView 实例设置“title”属性:

    UIAlertView * alertView = [[UIAlertView allow] init...];
    alertView.title = @"Some message here.";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多