2011-09-29 90 views
0

我有一个UIPicker,当选中一行时触发UIAlert。我试图在UIPicker“完成”按钮被按下并且UIPicker关闭后弹出警报。此时警报会在选中该行时触发。所以,当某人在选择器中滚动每一行时,UIAlert会一直弹出。UIPlerter关闭UIPicker后弹出

感谢您的帮助

这里的“完成”按钮代码:

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

}

这里的选择器UIAlert代码样本显示“的情况下0”与所述提示消息一起:

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

}

+0

为什么您在选择该行时触发警报?按下时完成。 – Akshay

+0

我不认为UIPickerView默认内置了一个“完成”按钮(或任何按钮)。 – chown

+0

我会怎么做呢?我有一系列开关(行){0}:}情况下触发警报。有没有办法保持这些,只有按'完成'时才触发警报? – hanumanDev

回答

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; 
} 

} 
+0

感谢您的帮助。完美地工作! – hanumanDev

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:)] 

那么你的行动:

// 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 [email protected]"Row %f selected!", selectedIndex + 1] message:[NSString stringWithFormat:@"Row %d/%d selected.", selectedIndex + 1, [self pickerView:myPickerView numberOfRowsInComponent:0]] autorelease]; 
    [alert show]; 
} 
0

优雅的方式: 添加委托给您的观看动画看到UIView.h

  • (无效)setAnimationDidStopSelector:(SEL)选择器
[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]; 

和实施此方法:

- (空)animationFinished:(的NSString *)animationID完成:(BOOL)完成背景:(void *的)情况下