0

我是编程新手,正如主题所暗示的那样iOS,我有一个问题。UIAlertViews

我有一个按钮可以删除SQLite DB. 此按钮调用 aUIAlertview以在删除记录时为用户提供一些不同的选项。

- (IBAction)deleteFunction:(id)sender {
UIAlertView *delChoice = [[UIAlertView alloc] initWithTitle:@"Select from below." message:@"WARNING!:You are about to remove records. This is irreversible." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete Completed jobs",@"Delete All records",@"Select items to delete.", nil];
[delChoice show];
}

这是下一个方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title =[alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Delete All records"]){
    [database executeUpdate:@"delete from issues"];

    if([database lastErrorCode]!=NULL){
        UIAlertView *unconfirm = [[UIAlertView alloc]initWithTitle:@"Failure!" message:@"Something went wrong. Try one more time." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [unconfirm show];
    }else {
        UIAlertView *confirm = [[UIAlertView alloc]initWithTitle:@"Success!" message:@"All records have been removed." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [confirm show];
    }
}

这不是完整的方法。我的问题是,在嵌套的 uialertview(取消确认和确认)中,如果我决定添加“otherButtonTitles”,我该如何回应?我是否使用相同的主要方法做同样的事情?

另外,如果有更好的方法可以做到这一点,我将不胜感激!

4

4 回答 4

1

我能想到的最好的方法就是

- (IBAction)deleteFunction:(id)sender 
{
    UIAlertView *delChoice = [[UIAlertView alloc] initWithTitle:@"Select from below." 
                                                        message:@"WARNING!:You are about to remove records. This is irreversible." 
                                                       delegate:self 
                                              cancelButtonTitle:@"Cancel" 
                                              otherButtonTitles:@"Delete Completed jobs",@"Delete All records",@"Select items to delete.", nil];

    [delChoice setTag:1]; // Setting the tag can help determine which view has come into a method call.
    [delChoice show];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{

    switch([alertView tag]) {
        case 1:
              NSString *title =[alertView buttonTitleAtIndex:buttonIndex];
              if([title isEqualToString:@"Delete All records"]){
                  [database executeUpdate:@"delete from issues"];

                  if([database lastErrorCode]!=NULL){
                      UIAlertView *unconfirm = [[UIAlertView alloc] initWithTitle:@"Failure!" 
                                                                     message:@"Something went wrong. Try one more time." 
                                                                    delegate:self 
                                                           cancelButtonTitle:@"ok" 
                                                           otherButtonTitles:nil];
                      [unconfirm setTag:2];
                      [unconfirm show];
                   } else {
                      UIAlertView *confirm = [[UIAlertView alloc] initWithTitle:@"Success!" 
                                                                     message:@"All records have been removed." 
                                                                    delegate:self 
                                                           cancelButtonTitle:@"ok" 
                                                           otherButtonTitles:nil];
                     [confirm setTag:3];
                     [confirm show];
                   }
             }
         break;
         case 2:
                // Do what you wish here for if UIAlertView has tag 2
                break;
         case 3:
                // Do what you wish here for if UIAlertView has tag 3
                break;
         default:
                // If you have any other UIAlertViews that have some basic default functionality you can do that here.
                break;
     }
}

那么这里发生了什么?我们正在设置每个UIAlertViews标签,当我们按下任何一个按钮时,UIAlertViews我们将进入此alertView: clickedButtonAtIndex:方法。所以我们使用UIAlertViews我们设置的标签来选择正确的大小写switch statement。我们获取UIAlertViews标签并选择正确的大小写,所以如果([alertView tag] == 1)那时我们会1为另一个做 case 等等UIAlertViews。然后我们有一个默认案例,因此如果您决定需要另一个UIAlertView案例,您可以添加另一个案例,case 3或者您可以使用默认案例来处理默认功能。如果您错过了每个案例结束时的休息时间,请不要忘记break;最后,该语句将继续到任何情况下低于该情况。希望对您有所帮助,如果您有任何问题,请发表评论。

于 2013-05-24T07:35:07.283 回答
0

您可以在 UIAlertview 中添加更多按钮,但tag对每个 alertview 使用属性以相互区分。像-

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"FlurtR" message:ERROR_INTERNET delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alert.tag=11;
[alert show];

当您必须评估时,您已按下确定或取消按钮,请使用此

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==11 && buttonIndex==0)
    {
        [self hideIndicator];
        checkForAlertView=0;
    }
}

您还可以tag在方法内定义属性, - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex在任何UIAlertView您将检查警报标记和按钮索引的地方都没有任何问题。

希望这可以帮助。

于 2013-05-24T07:25:21.013 回答
0
 // Yes you can do with this same thing but i suggest you to compare with ButtonIndex instead of string value like

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   if (buttonIndex == 0) {
       NSLog(@"Clicked button index 0");
      // Add the action here
   } else {
       NSLog(@"Clicked button index other than 0");
      // Add another action here
   }
} 
于 2013-05-24T07:26:42.143 回答
0

如果您有多个 UIAlertViews,则应首先区分 UIAlertViews。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if([alertView.title isEqualToString:@"Select from below."]){
NSString *title =[alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Delete All records"]){
    [database executeUpdate:@"delete from issues"];

    if([database lastErrorCode]!=NULL){
        UIAlertView *unconfirm = [[UIAlertView alloc]initWithTitle:@"Failure!" message:@"Something went wrong. Try one more time." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [unconfirm show];
    }else {
        UIAlertView *confirm = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"All records have been removed." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [confirm show];
    }
}
else if([alertView.title isEqualToString:@"Failure!"])
{
  // do your stuff for the failure alertView
}

}

于 2013-05-24T07:27:22.857 回答