将buttonIndex保存到对象(NSNumber)

时间:2011-11-11 03:11:05

标签: iphone nsnumber

我正在尝试将actionsIndex的动作表保存到一个对象中。但为什么它总是读为0?

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSNumber *type, *count;
    if ([actionSheet.title isEqualToString:@"Select Taxi Type"]) {
        if (buttonIndex !=3) {
            type = [NSNumber numberWithInteger:buttonIndex];
            UIActionSheet *taxiCountQuery = [[UIActionSheet alloc] initWithTitle:@"Select Taxi Count" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"1", @"2", @"3", nil];

            taxiCountQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
            taxiCountQuery.tag = actionSheet.tag;

            [taxiCountQuery showFromTabBar:self.tabBarController.tabBar];

            [taxiCountQuery release];
        }
    }
    else if ([actionSheet.title isEqualToString:@"Select Taxi Count"]){
        if (buttonIndex !=3) {
            NSLog(@"buttonIndex:%i", buttonIndex);
            count = [NSNumber numberWithInteger:buttonIndex];
            NSLog(@"type:%f", type); // always read 0
            NSLog(@"count:%f", count); // always read 0
        }
    }
}

编辑 - 第二部分

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSNumber *type = [[NSNumber alloc] init];
    if ([actionSheet.title isEqualToString:@"Select Taxi Type"]) {
        if (buttonIndex !=3) {
            type = [NSNumber numberWithInteger:buttonIndex];
            UIActionSheet *taxiCountQuery = [[UIActionSheet alloc] initWithTitle:@"Select Taxi Count" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"1", @"2", @"3", nil];
            taxiCountQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
            taxiCountQuery.tag = actionSheet.tag;

            [taxiCountQuery showFromTabBar:self.tabBarController.tabBar];

            [taxiCountQuery release];
        }
    }
    else if ([actionSheet.title isEqualToString:@"Select Taxi Count"]){
        if (buttonIndex !=3) {
            NSNumber *count = [[NSNumber alloc] initWithInteger:buttonIndex+1];
            NSLog(@"buttonIndex:%i", buttonIndex);
            count = [NSNumber numberWithInteger:buttonIndex +1];
           NSLog(@"type:%i", [type intValue]); // always read 0
            NSLog(@"count:%i", [count intValue]); // reads fine now
           [count release];
        }
    }
}

2 个答案:

答案 0 :(得分:1)

NSNumber是一个可以保存基本类型的对象,用于存储NSDictionary和NSArray等数据结构。 NSLog中的%f正在寻找双倍。 这段代码应该给你一个警告。

如果您使用NSLog(@“%i”,[输入intValue]),您将得到正确答案。

答案 1 :(得分:0)

请查看此代码。

NSNumber *numberNs = [[NSNumber alloc] initWithInteger:buttonIndex];

希望这会对你有所帮助。