Search code examples
objective-cios7uiactionsheet

ActionSheet clickedButtonAtIndex being used by 2 different actions


Currently the following delegate listens to the selectedIndex works but takes action on another index that does not belong to its action.

Action1:

- (IBAction)sendPicture:(id)sender {

UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil
                                                        delegate:self cancelButtonTitle:@"Cancel"
                                          destructiveButtonTitle:nil
                                               otherButtonTitles:@"Take Photo",@"Choose Existing Photo",nil];

popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];


}

Action2:

- (IBAction)sendTip:(id)sender {


UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"How much you want to tip?"
                                                        delegate:self cancelButtonTitle:@"Cancel"
                                          destructiveButtonTitle:nil
                                               otherButtonTitles:@"5.00 $",@"10.00 $",@"15.00 $",@"25.00 $",@"50.00 $",nil];

popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];

}

Delegate that listens the SelectedIndex of both ActionSheet menues:

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

NSLog(@"Selected Index: %d",buttonIndex);

if (buttonIndex == 0)
{

    urlTip = [NSString stringWithFormat:@"http://api.site.ca/user-tip/%@/%@/%@/",_http_nickfrom, _http_nickto, @"1"];


    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                        message:@"Do you want to send 5.00 $?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Send", nil];
    [alertView show];


}
if (buttonIndex ==1){

    urlTip = [NSString stringWithFormat:@"http://api.site.ca/user-tip/%@/%@/%@/",_http_nickfrom, _http_nickto, @"2"];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                        message:@"Do you want to send 10.00 $?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Send", nil];
    [alertView show];

}

if(buttonIndex == 2){

    urlTip = [NSString stringWithFormat:@"http://api.site.ca/user-tip/%@/%@/%@/",_http_nickfrom, _http_nickto, @"3"];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                        message:@"Do you want to send 15.00 $?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Send", nil];
    [alertView show];

}

if(buttonIndex == 3){
    urlTip = [NSString stringWithFormat:@"http://api.site.ca/user-tip/%@/%@/%@/",_http_nickfrom, _http_nickto, @"4"];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                        message:@"Do you want to send 25.00 $?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Send", nil];
    [alertView show];

}

if(buttonIndex == 4){

    urlTip = [NSString stringWithFormat:@"http://api.site.ca/user-tip/%@/%@/%@/",_http_nickfrom, _http_nickto, @"5"];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                        message:@"Do you want to send 50.00 $?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Send", nil];
    [alertView show];

}




}

I want to be able to take the values of SendPicture and perform some actions instead of executing the index that belongs to the other Action.


Solution

  • There are a few ways to do this. The easiest way:

    • Save both of your UIActionSheets as your class's properties, say you saved those as photoActionSheet and tipActionSheet.
    • In the following delegate method

      -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
      

      compare the actionSheet like:

      if (actionSheet == self.photoActionSheet){
      
          //do something
      }
      

    The second way is to have another class implement the delegate method. I recommend the easier way, but it really depends on how you structure the project.