【问题标题】:IOS:Show alert only once if UISlider changes its valueIOS:如果 UISlider 更改其值,则仅显示一次警报
【发布时间】:2015-03-25 15:24:24
【问题描述】:

当用户滑动滑块时,我正在执行 uislider 的应用程序 haivng 功能,它会向前移动并在标签中更改其值。我已经为此采取了这样的行动

- (IBAction)sensivity:(UISlider*)sender
{
    self.senlabel.text =[NSString stringWithFormat:@"%d", (int)sender.value];
} 

到这里为止很好,但我需要在用户第一次开始点击滑块时显示警报视图。如果用户单击确定,则它的标签应该更改滑块值,如果取消它应该显示一些默认值。 需要的关键点:

  1. 如果用户点击第二次,则仅显示一次警报现在应该显示警报
  2. 如果在警报视图上单击确定,那么只有滑块应该改变
  3. 如果在警报视图上单击取消,则滑块不应更改其值

【问题讨论】:

    标签: ios objective-c uialertview uislider uicontrolevents


    【解决方案1】:

    1) 查看dispatch_once API。查看this link

    2) 和 3) 在将警报抛出实例变量之前保存滑块的值。将您的类设置为 UIAlertView 的代表。如果点击取消按钮,请将滑块设置回保存的值。如果点击 OK 按钮(您必须在创建警报时指定),则什么也不做。

    有关 UIKit 入门,请参阅 Ray Wenderlich 的 site

    【讨论】:

    【解决方案2】:

    这是你的答案:

    > - (IBAction)valueChanged:(UISlider *)sender
    {
    static dispatch_once_t onceToken;
    
    dispatch_once (&onceToken, ^{
    
        UIAlertController * alert=   [UIAlertController
                                      alertControllerWithTitle:@"Title"
                                      message:@"Your custom message"
                                      preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"Cancel"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 //Do some thing here
                                 [alert dismissViewControllerAnimated:YES completion:nil];
                                 return ;
    
                             }];
        [alert addAction:cancel];
    
        UIAlertAction* ok = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 //Do some thing here
                                 [alert dismissViewControllerAnimated:YES completion:nil];
    
                             }];
        [alert addAction:ok];
    
        [self presentViewController:alert animated:YES completion:nil];
    
    });
    
    }
    

    【讨论】:

    • 如果包含解释,答案会更好。不要只是发布代码,解释它的作用和原因。
    【解决方案3】:

    我宁愿让你的 viewController 成为 UISlider 的代表,并管理自己的状态以显示/隐藏警报。

    检查你需要实现的 UISliderDelegate 方法(如果你不这样做编译器会抱怨)。

    @interface YourViewController : UIViewController <UIScrollViewDelegate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多