2013-08-26 145 views
1

我有UIActionSheet。它显示数组中的标题。当我点击按钮时,什么都不会发生。我也使用NSLog。但它没有显示任何东西。按钮标题正在从数组中显示。但是,只有点击不工作UIActionSheet按钮不起作用

代码:

ActionSheet:

TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"Design"]; 

     for (int i = 0; i<[catearray count]; i++) { 

      [actionSheet addButtonWithTitle:[catearray objectAtIndex:i] block:^{ 





       }]; 

     } 

按钮点击:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 

     NSLog(@"button"); 



    } else if (buttonIndex == 1) { 

     NSLog(@"button 1"); 


    } else if (buttonIndex == 2) { 


      NSLog(@"button 2"); 

    } else if (buttonIndex == 3) { 

    } 





} 

阵:

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 

      const char *sql = "SELECT id,cat_name FROM categories"; 

      NSLog(@"sql is %s",sql); 

      sqlite3_stmt *statement; 
      // int catID = 0; 
      if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) { 
       // We "step" through the results - once for each row. 
       while (sqlite3_step(statement) == SQLITE_ROW) { 


        category = [[NSString alloc] initWithUTF8String: 
           (const char *) sqlite3_column_text(statement, 1)]; 
        NSLog(@"catName is %@",category); 

        [catearray addObject:category]; 


        // catID = sqlite3_column_int(statement, 0); 
       } 
      } 


      sqlite3_finalize(statement); 
     } 

     else { 
      sqlite3_close(database); 
      NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); 
      // Additional error handling, as appropriate... 
     } 
+0

您需要设置委托以使委托方法有效。 – Desdenova

+0

如何?我使用TSPopoverActionsheet。这是第三方。如果我使用单独的按钮,它的工作。它使用数组它不工作 – user2674668

回答

1

您使用的是被称为TSActionSheet和行动图书馆在默认UIActionSheetDelegate不叫

所以我只好到你的动作移动到一个方法是这样

-(void)actionSheetClickedButtonAtIndex:(int)buttonIndex { 

    if (buttonIndex == 0) { 
     NSLog(@"button"); 



    } else if (buttonIndex == 1) { 

     NSLog(@"button 1"); 


    } else if (buttonIndex == 2) { 


      NSLog(@"button 2"); 

    } else if (buttonIndex == 3) { 

    } 
} 

然后调用此方法像这样的块

TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"Design"]; 

     for (int i = 0; i<[catearray count]; i++) { 

      [actionSheet addButtonWithTitle:[catearray objectAtIndex:i] block:^{ 

        [self actionSheetClickedButtonAtIndex:i]; 



       }]; 

     } 
+0

它的工作。谢谢 – user2674668

+0

[catearray objectAtIndex:i]来自webservice。如果数组越来越如何设置buttonIndex?如果我有2个按钮的catearray。所以我使用buttonIndex 0,1。发布应用程序后,然后我增加webservice数组如何设置按钮索引为该更新的按钮 – user2674668

+0

只要你使用循环...按钮将自动设置增量索引... 0,1,2,3 ,4,5,6,...,等等。但你必须处理actionSheetClickedButtonAtIndex函数中的新按钮功能,以采取正确的行动 –

1

不,你不是使用UIActionSheet,你是​​你唱TSActionSheet不是UIActionSheet的子类(It's a subclass of UIView),所以没有期望它实现UIActionSheetDelegate协议。请注意,当您向TSActionSheet添加按钮时,传递给按钮的参数之一是块。这是您放置您希望在按钮点击时执行的代码的地方。

[actionSheet addButtonWithTitle:@"button one" color:[UIColor whiteColor] titleColor:[UIColor darkGrayColor] borderWidth:1 borderColor:[UIColor grayColor] block:^{ 
     NSLog(@"pushed button one"); 
}]; 
+0

[catearray objectAtIndex:i]来自webservice。如果数组越来越如何设置buttonIndex?如果我有2个按钮的catearray。所以我使用buttonIndex 0,1。发布应用程序后,然后我增加webservice中的数组如何设置buttonIndex更新的按钮 – user2674668