2011-11-11 4 views
0

actionsheetの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 
     } 
    } 
} 

EDIT - 第二の部分

-(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はダブルを探しています。 このコードは警告を表示する必要があります。

NSLog(@ "%i"、[type intValue])を実行すると、正しい答えが得られます。

+0

何とかNSLogステートメントを変更するだけでクラッシュします。 –

+0

実際にタイプを何かに初期化する必要があります。もしそれがブロックの2番目の時にはあなたはいません。 – Randall

+0

編集した内容を含めるように質問を編集しましたが、まだ「タイプ」が機能するようになりました。 –

0

このコードをチェックアウトしてください。

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

希望します。

関連する問題