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 *的)情況下