我想将 UISlider 值的 value 属性更改为其二进制形式。
至于我做了什么:
-(IBAction)setValue:(id)sender
{
int value =(int)([sliderValue value] *200);
NSLog(@"slider value int %i", value);
NSLog(@"hex 0x%02X",(unsigned int)value);
NSMutableArray *xx;
[xx addObject:[NSNumber numberWithInt:value]];
NSLog(@"%@",xx);
NSInteger theNumber = [[xx objectAtIndex:value]intValue];
NSLog(@"%@",theNumber);
NSMutableString *str = [NSMutableString string];
NSInteger numberCopy = theNumber; // so won't change original value
for(NSInteger i = 0; i < 8 ; i++) {
// Prepend "0" or "1", depending on the bit
[str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
numberCopy >>= 1;
}
NSLog(@"Binary version: %@", str);
}
但是,有一个问题。每当滑块值更改时,它都会转换为整数和十六进制,但不会转换为二进制。谁能帮我找出我犯错的地方?