【问题标题】:How to create Iphone alert with items selection?如何使用项目选择创建 Iphone 警报?
【发布时间】:2011-11-12 16:32:49
【问题描述】:

如何为 iPhone 创建这样的警报?

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    您必须自己创建一个这样的视图。

    我能想到的最简单的方法是创建一个 UIView,然后以模态方式呈现它。

    【讨论】:

      【解决方案2】:

      为了做同样的事情,你必须使用嵌入 UITableView 的自定义 UIAlertView。为此,thisthis 将很好地指导您。

      【讨论】:

        【解决方案3】:
        【解决方案4】:

        在 iPhone 中,UIPickerViewUIActionSheet 的组合大多用于从用户那里获取输入,用户必须从多个选项中选择一个。

        一个例子:

        请参考this SO post 了解如何制作。

        【讨论】:

          【解决方案5】:

          为此,您只需要创建视图——它由 uiview、tableview 和一个按钮组成。 CustomAlertView 的代码。

          UIView-AlertAnimations.h                    // user defined class
          
          
          @interface UIView(AlertAnimations)
          
          - (void)doPopInAnimation;
          - (void)doPopInAnimationWithDelegate:(id)animationDelegate;
          - (void)doFadeInAnimation;
          - (void)doFadeInAnimationWithDelegate:(id)animationDelegate;
          @end
          

          UIView-AlertAnimations.m
          
          #import "UIView-AlertAnimations.h"
          #import <"QuartzCore/QuartzCore.h">
          
          #define kAnimationDuration  0.2555
          
          @implementation UIView(AlertAnimations)
          - (void)doPopInAnimation
          {
              [self doPopInAnimationWithDelegate:nil];
          }
          - (void)doPopInAnimationWithDelegate:(id)animationDelegate
          {
              CALayer *viewLayer = self.layer;
              CAKeyframeAnimation* popInAnimation =[CAKeyframeAnimation    
              animationWithKeyPath:@"transform.scale"];
          
          popInAnimation.duration = kAnimationDuration;
          popInAnimation.values = [NSArray arrayWithObjects:
                                   [NSNumber numberWithFloat:0.6],
                                   [NSNumber numberWithFloat:1.1],
                                   [NSNumber numberWithFloat:.9],
                                   [NSNumber numberWithFloat:1],
                                   nil];
          popInAnimation.keyTimes = [NSArray arrayWithObjects:
                                     [NSNumber numberWithFloat:0.0],
                                     [NSNumber numberWithFloat:0.6],
                                     [NSNumber numberWithFloat:0.8],
                                     [NSNumber numberWithFloat:1.0], 
                                     nil];    
          popInAnimation.delegate = animationDelegate;
          
          [viewLayer addAnimation:popInAnimation forKey:@"transform.scale"];  
          }
          
          - (void)doFadeInAnimation
          {
              [self doFadeInAnimationWithDelegate:nil];
          }
          - (void)doFadeInAnimationWithDelegate:(id)animationDelegate
          {
              CALayer *viewLayer = self.layer;
              CABasicAnimation *fadeInAnimation = [CABasicAnimation 
                                                      animationWithKeyPath:@"opacity"];
              fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0];
              fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
              fadeInAnimation.duration = kAnimationDuration;
              fadeInAnimation.delegate = animationDelegate;
              [viewLayer addAnimation:fadeInAnimation forKey:@"opacity"];
          }
          @end
          
          
          
          CustomAlertView.m
          
          @implementation CustomAlertView
          
          - (IBAction)show
          {
          // Retaining self is odd, but we do it to make this "fire and forget"
          [self retain];
          
          // We need to add it to the window, which we can get from the delegate
          id appDelegate = [[UIApplication sharedApplication] delegate];
          UIWindow *window = [appDelegate window];
          [window addSubview:self.view];
          
          // Make sure the alert covers the whole window
          self.view.frame = window.frame;
          self.view.center = window.center;
          
          // "Pop in" animation for alert
          [alertView doPopInAnimationWithDelegate:self];
          
          // "Fade in" animation for background
          [backgroundView doFadeInAnimation];
          }
          
          - (IBAction)dismiss:(id)sender
          {
              [inputField resignFirstResponder];
              [UIView beginAnimations:nil context:nil];
              self.view.alpha = 0.0;
              [UIView commitAnimations];
          
              [self performSelector:@selector(alertDidFadeOut) withObject:nil afterDelay:0.5];
          
          
          
          if (sender == self || [sender tag] == CustomAlertViewButtonTagOk)
              [delegate CustomAlertView:self wasDismissedWithValue:inputField.text];
          else
          {
              if ([delegate respondsToSelector:@selector(customAlertViewWasCancelled:)])
                  [delegate customAlertViewWasCancelled:self];
          } 
          

          }

           - (void)alertDidFadeOut
          {    
          [self.view removeFromSuperview];
          [self autorelease];
          }
          

          CAAnimation 委托方法

          - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
          {
              [self.inputField becomeFirstResponder];
          }
          
          - (BOOL)textFieldShouldReturn:(UITextField *)textField
          {
              [self dismiss:self];
              return YES;
          }
          @end
          
          
          - (IBAction)show
          {
          // Retaining self is odd, but we do it to make this "fire and forget"
          [self retain];
          
          // We need to add it to the window, which we can get from the delegate
          id appDelegate = [[UIApplication sharedApplication] delegate];
          UIWindow *window = [appDelegate window];
          [window addSubview:self.view];
          
          // Make sure the alert covers the whole window
          self.view.frame = window.frame;
          self.view.center = window.center;
          
          // "Pop in" animation for alert
          [alertView doPopInAnimationWithDelegate:self];
          
          // "Fade in" animation for background
          [backgroundView doFadeInAnimation];
          }
          
          
          - (IBAction)dismiss:(id)sender
          {
          [inputField resignFirstResponder];
          [UIView beginAnimations:nil context:nil];
          self.view.alpha = 0.0;
          [UIView commitAnimations];
          
          [self performSelector:@selector(alertDidFadeOut) withObject:nil afterDelay:0.5];
          
          if (sender == self || [sender tag] == CustomAlertViewButtonTagOk)
              [delegate CustomAlertView:self wasDismissedWithValue:inputField.text];
          else
          {
              if ([delegate respondsToSelector:@selector(customAlertViewWasCancelled:)])
                  [delegate customAlertViewWasCancelled:self];
          } 
          }
          
          
          - (void)alertDidFadeOut
          {    
              [self.view removeFromSuperview];
              [self autorelease];
          }
          
          
          - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
          {
              [self.inputField becomeFirstResponder];
          }
          

          最后,我们实现了一种文本字段委托方法,以便当用户按下键盘上的返回键时,它会关闭对话框。

          - (BOOL)textFieldShouldReturn:(UITextField *)textField
          {
              [self dismiss:self];
              return YES;
          }
          

          至此,我们完成了。我们现在可以像使用 UIAlertView 一样使用这个自定义警报视图:

           CustomAlertView *alert = [[CustomAlertView alloc]init];
          alert.delegate = self;
          [alert show];
          [alert release];
          

          当然这对你有用。

          【讨论】:

            猜你喜欢
            • 2015-06-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多