2011-11-11 66 views
0

我正在嘗試將操作表的selectedIndex保存到對象中。但爲什麼它總是讀0?將buttonIndex保存到對象(NSNumber)

-(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 
     } 
    } 
} 

編輯 - 第2部分

-(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]; 
     } 
    } 
} 

回答

1

NSNumber是一個對象,它可以存放基本類型,用於存儲像NSDictionary和NSArray這樣的數據結構。 NSLog中的%f正在尋找一個double。 這段代碼應該給你一個警告。

如果你做NSLog(@「%i」,[type intValue]),你會得到正確的答案。

+0

不知何故,只是改變NSLog語句崩潰。 –

+0

您需要實際將類型初始化爲某種東西。你不是當它在第二塊如果塊。 – Randall

+0

我編輯了我的問題,包括我編輯的內容,但仍然可以使用「類型」工作。 –

0

請看看這個代碼。

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

希望這可以幫助你。