2012-07-06 122 views
1

我正在尝试构建一个掷骰子的应用程序。没有什么奇特的。但是,我正在使用分段控制来确定骰子的数量和骰子的边。SegmentedControl始终为零

这里是RollPressed的代码。

#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那里,我总是使用零。当然,使用这种逻辑滚入显示我像3.82652e-308. 用来掷骰子的逻辑显示骰子结果促动是

- (int)RollDice:(int)dice Sides:(int)num_sides{ 
    total = 0; 

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

    return total; 
} 

什么会导致分段控制给我这样时髦的结果吗?

+0

分段控制是通过IB连接起来的吗?它是一个属性,然后您以编程方式设置自己? – bgoers 2012-07-06 00:18:41

+0

我把部分钩在IB上,我设法以某种方式错误地将它们连接起来。一个错误,我检查,虽然是好的,但StackOverflow让我再次检查并发现问题! – Linell 2012-07-06 00:30:25

回答

1

听起来像你的IBOutlets sidesdice没有连接。

检查它们的值;它可能是NULL。

+0

我以错误的方式连接了它们,您说的没错。 – Linell 2012-07-06 00:29:41