0

我有一个 UIPicker 在选择一行时触发 UIAlert 。我试图在按下 UIPicker“完成”按钮并关闭 UIPicker 后弹出警报。At the moment the alert triggers when the row is selected. 因此,当有人滚动选择器中的每一行时,UIAlert 会不断弹出。

谢谢你的帮助

这是“完成”按钮代码:

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

}

这是显示“案例 0”以及警报消息的选择器 UIAlert 代码示例:

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

}

4

3 回答 3

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 stringWithFormat@"Row %f selected!", selectedIndex + 1] message:[NSString stringWithFormat:@"Row %d / %d selected.", selectedIndex + 1, [self pickerView:myPickerView numberOfRowsInComponent:0]] autorelease];
    [alert show];
}
于 2011-09-29T04:13:01.997 回答
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;
}

}
于 2011-09-29T05:29:47.093 回答
0

优雅的方式:将委托添加到您的视图动画中,请参阅 UIView.h

  • (void)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]; 

并实现这个方法:

-(void)animationFinished:(NSString *)animationID完成:(BOOL)完成上下文:(void *)上下文

于 2011-09-29T08:57:46.387 回答