Search code examples
iphonearraysuiactionsheet

UIActionSheet button is not working


I have UIActionSheet. It displays the title from array. When i click the button is nothing will happen. I used NSLog also. But it's not displaying anything. Button title is displaying from array. But only click is not working

code:

ActionSheet:

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

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

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





              }];

         }

button click:

-(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) {

    }





}

Array:

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

Solution

  • You are using a library called TSActionSheet and actions not called in default UIActionSheetDelegate

    so u have to to move your action to a method like this

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

    then call this method on your block like this

    TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"Design"];
    
             for (int i = 0; i<[catearray count]; i++ ) {
    
                 [actionSheet addButtonWithTitle:[catearray objectAtIndex:i] block:^{
    
                        [self actionSheetClickedButtonAtIndex:i];
    
    
    
                  }];
    
             }