ActionSheet clickedButtonAtIndex被2个不同的动作使用

时间:2014-08-18 02:51:01

标签: objective-c ios7 uiactionsheet

目前,以下委托侦听selectedIndex工作,但对另一个不属于其操作的索引执行操作。

措施1:

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


}

措施2:

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

}

侦听两个ActionSheet menues的SelectedIndex的委托:

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

}




}

我希望能够获取SendPicture的值并执行一些操作,而不是执行属于另一个Action的索引。

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。最简单的方法:

  • 将您的两个UIActionSheets保存为您的课程属性,比如说您将其保存为photoActionSheettipActionSheet
  • 在以下委托方法

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

    比较actionSheet,如:

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

第二种方法是让另一个类实现委托方法。我建议采用更简单的方法,但这实际上取决于你如何构建项目。