UIAlertView 中的 iOS UISlider

标签 ios uialertview uislider

我知道有一个关于此的问题,但我不清楚。我想向 UIAlertView 添加一个 UISlider。在 UIAlertView 中,我还添加了一个 UILabel 来显示 UISlider 的值。到目前为止我所做的是:

            UIAlertView *alert= [[UIAlertView alloc]
                    initWithTitle: @"Set delay"
                    message: @" "
                    delegate: nil
                    cancelButtonTitle:@"OK"
                    otherButtonTitles:nil];
            UISlider *mySlider=[[UISlider alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
            mySlider.maximumValue=10.00;
            mySlider.minimumValue=1.00;
            UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(220, 50, 50, 40)];
            int x=.........;//x should get value of mySlider
            [myLabel setText:[NSString stringWithFormat:@"%d",x]];
            [alert addSubview:myLabel];
            [alert addSubview:mySlider];            
            [alert show];
            [alert release];

所以我希望变量 x 获取 mySlider 的值并且 myLabel 将显示 x 的值.我被困在这一点上。非常感谢任何建议。

最佳答案

这里是你需要做的:

  1. 使 UILabel 成为全局的,这意味着在头文件中声明它(这是为了轻松到达标签)。
  2. 然后使用 ValueChanged 事件将目标添加到 slider 。

    [mySlider addTarget:self action:@selector(sliderHandler:) forControlEvents:UIControlEventValueChanged];
    
  3. 接下来,实现sliderHandler方法:

    - (void) sliderHandler: (UISlider *)sender {
         int x = sender.value;
         [myLabel setText:[NSString stringWithFormat:@"value = %d",x]];
    }
    

这对你有用。

关于UIAlertView 中的 iOS UISlider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11698217/

相关文章:

ios - 检查 iOS 6 中是否安装了 Google map 应用程序

ios - 锁定屏幕中的 AlertView 显示?

ios - 如何使用步进 slider 在 slider 拇指上添加 UILabel?

ios - swift 4 : NSPredicate from UITextField and array of custom object

ios - 如何获得一个按钮来确定要转到哪个 View Controller ?

ios - 如何获取 "yyyy-MM-dd' T'HH :mm:ss. SSSZ"格式的日期?

iphone - 一段时间后显示 UIAlertView

ios - 在 1 到 100 的范围内将 UISlider 递增 1

ios - UISlider - 将 UILabel 添加到最小/最大 UIImageView?

ios - AdMob iOS,在测试设备上展示广告