【问题标题】:handling two alertview处理两个警报视图
【发布时间】:2012-05-21 05:14:30
【问题描述】:

我正在申请

1) 我显示一个警报视图来接受或拒绝来电....

2) 但是,如果呼叫者自己取消了呼叫,则会显示一条警报,说明呼叫者已取消呼叫。

我的问题是,如果在我接受之前取消呼叫,警报视图会堆叠,并且在警报视图更近的位置(2)我仍然可以看到警报视图(1),因为我的要求是直接显示查看任何警报视图的更近的位置。

我已经创建了一种生成警报视图的方法,我给警报视图提供了差异标签

-(void)generateMessage:(const char*)msg Title:(const char*)title withAcceptButton:(bool)doAddAcceptButton Tag:(int)tag{

dispatch_async(dispatch_get_main_queue(), ^{

                 // We are now back on the main thread
                 UIAlertView *alertView = [[UIAlertView alloc] >init];
                //add button

                 if(doAddAcceptButton==true)
                 {
                     [alertView  addButtonWithTitle:@"OK"];
                     [alertView addButtonWithTitle:@"Cancel"];
                     alertView.cancelButtonIndex=1;

                 }
                 else {
                     [alertView  addButtonWithTitle:@"OK"];
                     alertView.cancelButtonIndex=0;
                 }

                 //add tag
                 [alertView setTag:tag];

                 //add title
                 if(title==NULL)
                 {
                     [alertView setTitle:@"MESSAGE"];
                 }
                 else {
                     NSMutableString *head = [[NSMutableString >alloc] initWithCString:title
                                                                             >encoding:NSUTF8StringEncoding];
                     [alertView setTitle:head];
                     [head release];
                 }


                 if(msg==NULL)
                 {
                     [alertView setMessage:@"ERROR"];
                 }
                 else {
                     NSMutableString *body = [[NSMutableString >alloc] initWithCString:msg
                                                                             >encoding:NSUTF8StringEncoding];
                     [alertView setMessage:body];
                     [body release];
                 }
                 [alertView setDelegate:self];
                 [alertView show];
                 [alertView release];


             });

}

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    只需保留对警报视图的引用即可。这样,如果第一个仍在显示,您可以在显示第二个之前清除它。比如:

    .h 文件:

    @interface ViewController : UIViewController <UIAlertViewDelegate> {
      UIAlertView * _alertView1;
      UIAlertView * _alertView2;
    }
    

    .m 文件:

    - (void)viewDidLoad; {
      [super viewDidLoad];
      _alertView1 = [[UIAlertView alloc] initWithTitle:@"Alert 1" message:@"A New call!" delegate:self cancelButtonTitle:@"Deny" otherButtonTitles:@"Accept", nil];
      _alertView2 = [[UIAlertView alloc] initWithTitle:@"Alert 2" message:@"The Call was cancelled!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    }
    
    - (void)callWasCancelled; { // This would be the method where the second AlertView is called.
      if(_alertView1.isVisible){
        [_alertView1 dismissWithClickedButtonIndex:0 animated:YES];
      }
      [_alertView2 show];
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; {
      if(alertView == _alertView1){
        if(buttonIndex == 1){
          // Accept the call!
        }
      }
    }
    

    希望有帮助!

    【讨论】:

    • 保留参考有助于thanx
    • 没问题!很高兴我能帮上忙!
    【解决方案2】:

    您可以使用通知来实现这一点。当您意识到呼叫已被取消时,发出通知。处理通知时,关闭第一个 UIAlertView:

    - (void)callCancelled {
        // Fire the notification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CallCancelled" 
                                                            object:nil];   
    }
    

    处理“CallCancelled”通知:

    [NSNotificationCenter defaultCenter] addObserver:self 
                                            selector:@selector(handleCancelNotification:) 
                                                name:@"CallCancelled" 
                                              object:nil];
    
    - (void)handleCancelNotification:(id)object {
      // Dismiss the first AlertView.
      [alertView dissmissWithClickedButtonIndex:-1 animated:YES];
    }
    

    【讨论】:

      【解决方案3】:
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Confirm" message:@"Do you pick Yes or No?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
          [alert setDelegate:self];
          [alert show];
          [alert release];
          // Do any additional setup after loading the view, typically from a nib.
      }
      
      
      - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
      {
          if (buttonIndex == 0 && [alertView.title isEqualToString:@"Confirm"])
          {
              UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"Call is Cancelled" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
              [alert1 setDelegate:self];
              [alert1 show];
              [alert1 release];
      
          }
      }
      

      【讨论】:

        【解决方案4】:

        实际上,最好重新设计您的应用程序以避免 2 个 alertViews 显示一个在另一个顶部的可能性。

        【讨论】:

        • 那么你认为应该遵循什么样的设计
        • 例如,制作一个自定义视图,您可以在其中显示用户来电的当前状态。当电话进入电话时 - 在您的自定义视图中显示用户有来电。取消通话时 - 通过获取例如取消通话的通知来更新自定义视图。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-02
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多