Cocoa notes (26) UIAlertView
星期五, 六月 3rd, 2011基本
1 | [[UIAlertView alloc] initWithTitle:@"Title" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"One", @"Two", @"Three", nil]; |
无按钮Alert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | - (void) action: (UIBarButtonItem *) item { baseAlert = [[[UIAlertView alloc] initWithTitle:@"Please Wait" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; [baseAlert show]; // Create and add the activity indicator UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; aiv.center = CGPointMake(baseAlert.bounds.size.width / 2.0f, baseAlert.bounds.size.height - 40.0f); [aiv startAnimating]; [baseAlert addSubview:aiv]; [aiv release]; // Auto dismiss after 3 seconds [self performSelector:@selector(performDismiss) withObject:nil afterDelay:3.0f]; } - (void) performDismiss { [baseAlert dismissWithClickedButtonIndex:0 animated:NO]; } |
模态AlertView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CFRunLoopRef currentLoop = CFRunLoopGetCurrent(); // Create Alert ModalAlertDelegate *madelegate = [[ModalAlertDelegate alloc] initWithRunLoop:currentLoop]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:question message:nil delegate:madelegate cancelButtonTitle:button1 otherButtonTitles:button2, nil]; [alertView show]; // Wait for response CFRunLoopRun(); // Retrieve answer NSUInteger answer = madelegate.index; [alertView release]; [madelegate release]; |
显示模态prompt窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | +(NSString *) textQueryWith: (NSString *)question prompt: (NSString *)prompt button1: (NSString *)button1 button2:(NSString *) button2 { // Create alert CFRunLoopRef currentLoop = CFRunLoopGetCurrent(); ModalAlertDelegate *madelegate = [[ModalAlertDelegate alloc] initWithRunLoop:currentLoop]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:question message:@"\n" delegate:madelegate cancelButtonTitle:button1 otherButtonTitles:button2, nil]; // Build text field UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 30.0f)]; tf.borderStyle = UITextBorderStyleRoundedRect; tf.tag = TEXT_FIELD_TAG; tf.placeholder = prompt; tf.clearButtonMode = UITextFieldViewModeWhileEditing; tf.keyboardType = UIKeyboardTypeAlphabet; tf.keyboardAppearance = UIKeyboardAppearanceAlert; tf.autocapitalizationType = UITextAutocapitalizationTypeWords; tf.autocorrectionType = UITextAutocorrectionTypeNo; tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // Show alert and wait for it to finish displaying [alertView show]; while (CGRectEqualToRect(alertView.bounds, CGRectZero)); // Find the center for the text field and add it CGRect bounds = alertView.bounds; tf.center = CGPointMake(bounds.size.width / 2.0f, bounds.size.height / 2.0f - 10.0f); [alertView addSubview:tf]; [tf release]; // Set the field to first responder and move it into place [madelegate performSelector:@selector(moveAlert:) withObject:alertView afterDelay: 0.7f]; // Start the run loop CFRunLoopRun(); // Retrieve the user choices NSUInteger index = madelegate.index; NSString *answer = [[madelegate.text copy] autorelease]; if (index == 0) answer = nil; // assumes cancel in position 0 [alertView release]; [madelegate release]; return answer; } // Move alert into place to allow keyboard to appear - (void) moveAlert: (UIAlertView *) alertView { CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:0.25f]; if (![self isLandscape]) alertView.center = CGPointMake(160.0f, 180.0f); else alertView.center = CGPointMake(240.0f, 90.0f); [UIView commitAnimations]; [[alertView viewWithTag:TEXT_FIELD_TAG] becomeFirstResponder]; } |
Action sheet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { [actionSheet release]; [self say:@"User Pressed Button %d\n", buttonIndex + 1]; } -(void) action: (UIBarButtonItem *) item { UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle: @"File Management" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete File" otherButtonTitles:@"Rename File", @"Email File", nil]; [menu showInView:self.view]; } |
progressbar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // This callback fakes progress via setProgress: - (void) incrementBar: (id) timer { amountDone += 1.0f; [progressView setProgress: (amountDone / 20.0)]; if (amountDone > 20.0) { [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES]; self.actionSheet = nil; [timer invalidate]; } } // Load the progress bar onto an actionsheet backing -(void) action: (UIBarButtonItem *) item { amountDone = 0.0f; self.actionSheet = [[[UIActionSheet alloc] initWithTitle:@"Downloading data. Please Wait\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle: nil otherButtonTitles: nil] autorelease]; progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0.0f, 40.0f, 220.0f, 90.0f)]; [progressView setProgressViewStyle: UIProgressViewStyleDefault]; [actionSheet addSubview:progressView]; [progressView release]; // Create the demonstration updates [progressView setProgress:(amountDone = 0.0f)]; [NSTimer scheduledTimerWithTimeInterval: 0.35 target: self selector:@selector(incrementBar:) userInfo: nil repeats: YES]; [actionSheet showInView:self.view]; progressView.center = CGPointMake(actionSheet.center.x, progressView.center.y); } |