首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertView内部的UIAlertView

UIAlertView内部的UIAlertView
EN

Stack Overflow用户
提问于 2013-05-24 15:11:28
回答 4查看 1.2K关注 0票数 0

我是iOS编程的新手,我有一个关于UIAlertViews的问题,正如主题所建议的那样。

我有一个删除SQLite DB中记录的按钮。这些按钮调用一个UIAlertview,在删除记录时为用户提供几个不同的选项。

代码语言:javascript
复制
- (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];
}

下面是下一个方法

代码语言:javascript
复制
-(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“,我该如何响应它们?我是不是在同一个main方法中做了同样的事情?

另外,如果有更好的方法,我将不胜感激!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-05-24 15:35:07

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

代码语言:javascript
复制
- (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),那么我们将对另一个UIAlertViews执行case 1,依此类推。然后我们有一个默认的用例,所以如果你决定需要另一个UIAlertView,你可以添加另一个用例,这样case 3或者你可以用默认的用例来处理默认的功能。不要忘记每个案例结束时的空格,如果你错过了最后的break;,则语句将继续执行,无论哪个案例低于那个案例。希望对您有所帮助,如果您有任何问题,请留言。

票数 1
EN

Stack Overflow用户

发布于 2013-05-24 15:25:21

您可以在UIAlertview中添加更多按钮,但使用tag属性来区分每个警报视图。就像-

代码语言:javascript
复制
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"FlurtR" message:ERROR_INTERNET delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alert.tag=11;
[alert show];

当你必须评估的时候,你已经按下了OK或cancel按钮,使用这个

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==11 && buttonIndex==0)
    {
        [self hideIndicator];
        checkForAlertView=0;
    }
}

您还可以在任何UIAlertView中的- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法中定义tag属性,您可以在其中检查警报标记和按钮索引,而不会出现任何问题。

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2013-05-24 15:26:42

代码语言:javascript
复制
 // 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
   }
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16729491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档