1

i am doing app haivng functionality of uislider when user slides the slider it moves forword and chages its value in the label.I have given action like this for that

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

upto here its fine but i need to show the alert view when user starts to tap the slider for first time. if user click ok then it label should change the slider value if cancel it should show some default value . Key points needed:

  1. show alert for only time if user taps second time alert should now show
  2. if click ok on alert view then only slider should change
  3. if click cancel on alert view then slider should not change its value
KlimczakM
  • 12,576
  • 11
  • 64
  • 83
dev
  • 65
  • 1
  • 1
  • 7

3 Answers3

2

1) Look at the dispatch_once API. Check out this link.

2) and 3) Save the value of the slider right before you throw the alert up in an instance variable. Set your class to be the delegate of the UIAlertView. If the cancel button is hit, set the slider back to the saved value. If the OK button is hit (which you must specify when creating the alert), do nothing.

For a UIKit primer, see Ray Wenderlich's site.

diatrevolo
  • 2,782
  • 26
  • 45
  • 1
    it should be noted that UIAlertVIew's are deprecated in is and beyond, you should be using UIAlertController https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/ – bolnad Mar 26 '15 at 15:43
0

Here is your answer :

> - (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];

});

}
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
  • 2
    Answers are better if they include an explanation. Don't just post code, explain what it does and why. – rmaddy Mar 25 '15 at 17:11
0

I'd rather make your viewController be delegate of the UISlider and manage yourself the state of the same to show/hide alerts.

Check UISliderDelegate methods you need to implement (compiler will complain if you don't).

@interface YourViewController : UIViewController <UIScrollViewDelegate>
wolffan
  • 1,084
  • 10
  • 15