这是一个用于添加小费的自定义 UIActionSheet。它包含 UIPickerView 和用于处理按金额和百分比小费/小费的工具附件。选择小费/小费后,会调用 Delegate,并返回小费/小费的金额,总额和百分比。
MTGratuityPicker/Classes
目录复制到您的项目中。#import "MTTipViewController.h"
和 #import "MTTipViewControllerDelegate.h"
包含头文件
#import "MTTipViewController.h"
#import "MTTipViewControllerDelegate.h"
实现代理
@interface MTGratuityPickerExampleViewController : UIViewController <MTTipViewControllerDelegate>
{
}
创建 MTGratuityPicker 实例
NSDecimalNumber *total = [NSDecimalNumber decimalNumberWithString:@"25.50"];
MTTipViewController *tipController = [[MTTipViewController alloc] initWithSubtotalAndSelectedPercentage:total selectedPercentage:[NSDecimalNumber one]];
CGRect frame = CGRectMake(tipController.view.frame.origin.x,
self.view.frame.size.height,
tipController.view.frame.size.width,
tipController.view.frame.size.height);
tipController.view.frame = frame;
[tipController setDelegate:self];
[self addChildViewController:tipController];
[tipController viewWillAppear:YES];
[self.view addSubview:tipController.view];
[tipController viewDidAppear:YES];
[tipController didMoveToParentViewController:self];
在视图中显示 MTGratuityPicker
[tipController showInParentView];
实现代理方法 didSelectTipAmount
#pragma mark -
#pragma mark MTTipViewControllerDelegate Methods
- (void)didSelectTipAmount:(NSDecimalNumber *)tipAmount forTotalAmount:(NSDecimalNumber *)totalAmount atPercent:(int) percent
{
NSLog(@"Delegate Selected %@ for %@", tipAmount, totalAmount);
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[lblTotalAmount setText:[numberFormatter stringFromNumber:totalAmount]];
[lblTipAmount setText:[numberFormatter stringFromNumber:tipAmount]];
}
在 示例 文件夹中查看一个与 UIViewController 有关的完整示例。
您还可以重写标题和按钮文本。PickerView 使用 NSLocaleCurrencySymbol 作为货币符号。
[tipController setTitle:<#(NSString *)#>];
[tipController setAddTipPrompt:<#(NSString *)#>];
[tipController setCancelTipPrompt:<#(NSString *)#>];
MTGratuityPicker 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。