0

I have a UIPicker that triggers a UIAlert when a row is selected. I'm trying to have the alert popup after the UIPicker "done" button is pressed and the UIPicker is closed. At the moment the alert triggers when the row is selected. So, as someone scrolls through each row in the picker a UIAlert keeps popping.

thanks for any help

here's the 'done' button code:

-(IBAction)done{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 480);
    pickerView.transform = transform;
    [UIView commitAnimations];  

}

here's a sample of the picker UIAlert code showing 'case 0' along with the alert message:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

UIColor *newColor;
switch (row) {
    case 0:
        newColor = [UIColor yellowColor];       
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

        myLabel.text = @"sometext";
        break;

}

hanumanDev
  • 6,592
  • 11
  • 82
  • 146

3 Answers3

2
//Init the done button with an action to show an alert
// and the target as self
self.myBarBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemDefault target:self action:@selector(showAlert:)]

Then your action:

// Show the alert with the currently selected row index of the picker 
- (void)showAlert:(id)sender {
    NSUInteger selectedIndex = [myPickerView selectedRowInComponent:0];
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat@"Row %f selected!", selectedIndex + 1] message:[NSString stringWithFormat:@"Row %d / %d selected.", selectedIndex + 1, [self pickerView:myPickerView numberOfRowsInComponent:0]] autorelease];
    [alert show];
}
chown
  • 51,908
  • 16
  • 134
  • 170
1
-(IBAction)done{

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"message"     delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 480);
    pickerView.transform = transform;
    [UIView commitAnimations]; 
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

UIColor *newColor;
switch (row) {
    case 0:
        newColor = [UIColor yellowColor];       

        myLabel.text = @"sometext";
        break;
}

}
sicKo
  • 1,241
  • 1
  • 12
  • 35
0

Elegant way : Add delegate to your view animation see UIView.h

  • (void)setAnimationDidStopSelector:(SEL)selector
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)
[UIView setAnimationWillStartSelector:self];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 480);
pickerView.transform = transform;
[UIView commitAnimations]; 

and implement this method:

-(void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context

voilà