首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SegmentedControl始终为零

SegmentedControl始终为零
EN

Stack Overflow用户
提问于 2012-07-06 08:14:21
回答 1查看 66关注 0票数 1

我正在尝试构建一个掷骰子的应用程序。一点也不花哨。但是,我使用分段控件来确定骰子的数量和骰子的边。

这是RollPressed代码。

代码语言:javascript
复制
#import "DiceBrain.h"
#import "ViewController.h"

@interface ViewController ()
@property (readonly) DiceBrain *brain;
@end

@implementation ViewController

- (DiceBrain *) brain {
    if (!brain) brain = [[DiceBrain alloc] init];
    return brain;
}

- (IBAction)RollPressed:(id)sender {
    int num_dice = dice.selectedSegmentIndex;
    int num_sides = sides.selectedSegmentIndex;
    NSLog(@"Number of Dice is %u and Number of Sides is %u", num_dice, num_sides);
    int result = [self.brain RollDice:num_dice Sides: num_sides];
    display.text = [NSString stringWithFormat:@"%g", result];
}

@end

根据NSLog there的说法,我总是使用零。当然,使用此逻辑来启动掷骰子会在显示中显示类似于3.82652e-308.的内容。

代码语言:javascript
复制
- (int)RollDice:(int)dice Sides:(int)num_sides{
    total = 0;

    for (int i = 0; i < dice; i++) {
        total += (arc4random() % num_sides) + 1;
    }

    return total;
}

是什么原因导致分段控件会给我带来如此怪异的结果?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-06 08:18:34

听起来您的sidesdice IBOutlets未连接。

检查它们的值;它可能为空。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11354272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档