UIAlert всплывающее окно после закрытия UIPicker
У меня есть UIPicker, который запускает UIAlert при выборе строки. Я пытаюсь получить всплывающее окно с предупреждением после нажатия кнопки "Готово" UIPicker и закрытия UIPicker. В данный момент оповещение срабатывает при выборе строки. Таким образом, когда кто-то прокручивает каждую строку в средстве выбора, UIAlert продолжает появляться.
спасибо за любую помощь
Вот код кнопки "Готово":
-(IBAction)done{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 480);
pickerView.transform = transform;
[UIView commitAnimations];
}
Вот пример кода UIAlert средства выбора, показывающий "case 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;
}
3 ответа
Решение
-(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;
}
}
//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];
}
Элегантный способ: добавьте делегата в анимацию вашего просмотра, см. 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];
и реализовать этот метод:
- (void) animationFinished: (NSString *) animationID закончено:(BOOL) завершено контекст:(void *) контекст
voilà