Xcode字段+ [数字]除非最大值

时间:2015-04-27 05:32:25

标签: objective-c xcode macos cocoa

我有一些字段值,我希望设置最小值和最大值。第一个字段(_EvTotal)需要与第二个字段(_HpTotal)不同的最小值和最大值。

此时,按钮(例如,如下所示的HpPlusOne)可以无限次按下,但数字上没有上限。如果我希望为第一个字段(在这种情况下为_EvTotal)设置示例限制210,并为第二个字段设置示例限制112,那么我最好怎么做呢?

编辑:一旦数字达到上限,例如它是200并且有人按下50按钮,我怎么能让它达到上限而不是更高?到目前为止提供的解释只是简单地将字段设置为零(这是我的错误,因为没有正确解释&重置为零也可能是我犯了错误。)

- (IBAction)HpPlusOne:(id)sender {
    int counterNow, CounterLater;
    counterNow=[_EvTotal intValue];
    CounterLater=counterNow+1;
    [_EvTotal setIntValue:CounterLater];
    int hpNow, hpLater;
    hpNow=[_HpTotal intValue];
    hpLater=hpNow+1;
    [_HpTotal setIntValue:hpLater];

}

- (IBAction)HpPlusTwo:(id)sender {
    int counterNow, CounterLater;
    counterNow=[_EvTotal intValue];
    CounterLater=counterNow+2;
    [_EvTotal setIntValue:CounterLater];
    int hpNow, hpLater;
    hpNow=[_HpTotal intValue];
    hpLater=hpNow+2;
    [_HpTotal setIntValue:hpLater];
}

- (IBAction)HpPlusTen:(id)sender {
    int counterNow, CounterLater;
    counterNow=[_EvTotal intValue];
    CounterLater=counterNow+10;
    [_EvTotal setIntValue:CounterLater];
    int hpNow, hpLater;
    hpNow=[_HpTotal intValue];
    hpLater=hpNow+10;
    [_HpTotal setIntValue:hpLater];
}

- (IBAction)HpPlusFifty:(id)sender {
    int counterNow, CounterLater;
    counterNow=[_EvTotal intValue];
    CounterLater=counterNow+50;
    [_EvTotal setIntValue:CounterLater];
    int hpNow, hpLater;
    hpNow=[_HpTotal intValue];
    hpLater=hpNow+50;
    [_HpTotal setIntValue:hpLater];
}

最后道歉,如果已经回答,我在类似的问题上找到了其他答案,但它们是关于最终用户可直接编辑的文本字段。提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

问题:在将值分配给if之前,执行_HpTotal有多难?

- (NSInteger)limitHp:(NSInteger)hp toMax:(NSInteger)max andMin:(NSInteger)min {
    if (hp < max) {
        return max;
    }
    if (hp > min) {
        return min;
    }
    return hp;
}

  - (IBAction)HpPlusFifty:(id)sender {
    int counterNow, CounterLater;
    counterNow=[_EvTotal intValue];
    CounterLater=counterNow+50;
    [_EvTotal setIntValue:CounterLater];
    int hpNow, hpLater;
    hpNow=[_HpTotal intValue];
    hpLater=hpNow+50;
    NSInteger limitedHp = [self limitHp:hpLater toMax:MAX_HP andMin:MIN_TP];
    [_HpTotal setIntValue:limitedHp];
}

一方面,您的代码非常不好。你有4个函数(每个函数长10行),只有一个int不同。

- (IBAction)HpPlusOne:(id)sender {
    [self increaseHpWith:1];

}

 - (IBAction)HpPlusTwo:(id)sender {
    [self increaseHpWith:2];
}

 - (IBAction)HpPlusTen:(id)sender {
    [self increaseHpWith:10];
}

 - (IBAction)HpPlusFifty:(id)sender {
    [self increaseHpWith:50];
}

 - (void)increaseHpWith:(NSInteger)difference {
    int counterNow, CounterLater;
    counterNow=[_EvTotal intValue];
    CounterLater=counterNow+50;
    [_EvTotal setIntValue:CounterLater];
    int hpNow, hpLater;
    hpNow=[_HpTotal intValue];
    hpLater=hpNow+50;
    NSInteger limitedHp = [self limitHp:hpLater toMax:MAX_HP andMin:MIN_TP];
    [_HpTotal setIntValue:limitedHp];
}

答案 1 :(得分:1)

如果你想要一个更干净的解决方案,你可以为hp设置一个@property,然后用限制它的逻辑覆盖它的setter和getter,例如:

//define in your header
@property (nonatomic) int hp; 
@property int maxHp = 210;
@property int minHp = 0;

//in your .m file
- (void) setHp: (int) hp {
    _hp = hp;
    if(_hp > self.maxHp){
        _hp = self.maxHp //if its more than 210, set it to 210
    }
    if(_hp <= self.minHp){
        _hp = self.minHp;
        [self playerDeath]; //bonus of calling other useful methods from just setting the value of this variable
    }
}

稍后在您的代码中,您可以像

一样使用它
self.hp = 200;
self.hp += 20; //will be set to 210 actually
self.hp -= 500; //will be set to 0 and trigger [self playerDeath];

为每种不同类型的字段重复这些方法