我有这个segmented control,当点击其中一个段时,我想让它激活一些东西。我的应用程序已经做了一些计算,所以我希望分段控件只在被点击时激活。我创建了一个if statement来激活它,但我不确定如何设置条件。不确定我是否正确地使用了这个应用程序,但我只是在测试一些东西。
if (/** on tap **/){
NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)];
tipAmount = billAmount * [tipValues[self.tipControl.selectedSegmentIndex] floatValue];
totalAmount = tipAmount + billAmount;
}以下是我的项目的一部分代码:
#import "TipViewController.h"
#import "SettingsViewController.h"
@interface TipViewController ()
@property (weak, nonatomic) IBOutlet UITextField *billTextField;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
@property (weak, nonatomic) IBOutlet UISegmentedControl *tipControl;
- (IBAction)onTap:(id)sender;
- (void)updateValues;
- (void)onSettingsButton;
@end
@implementation TipViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"Tip Calculator";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateValues];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(onSettingsButton)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)onTap:(id)sender {
[self.view endEditing:YES]; //keyboard goes away
[self updateValues];
}
- (void)updateValues{
float billAmount = [self.billTextField.text floatValue];
float tipAmount;
float totalAmount;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//NSString *stringValue = [defaults objectForKey:@"some_key_that_you_choose"];
int intValue = [defaults integerForKey:@"another_key_that_you_choose"];
if (intValue && billAmount > 0) {
tipAmount = .01 * intValue * billAmount;
totalAmount = tipAmount + billAmount;
}
// array to hold all tip values
if (){
NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)];
tipAmount = billAmount * [tipValues[self.tipControl.selectedSegmentIndex] floatValue];
totalAmount = tipAmount + billAmount;
}
self.tipLabel.text = [NSString stringWithFormat:@"$%0.2f", tipAmount];
self.totalLabel.text = [NSString stringWithFormat:@"$%0.2f", totalAmount];
}
- (void)onSettingsButton {
[self.navigationController pushViewController:[[SettingsViewController alloc] init] animated:YES];
}
@end更新的updateValues
- (void)updateValues{
float billAmount = [self.billTextField.text floatValue];
//tipAmountIndex = NSNotFound;
float tipAmount;
float totalAmount;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//NSString *stringValue = [defaults objectForKey:@"some_key_that_you_choose"];
int intValue = [defaults integerForKey:@"another_key_that_you_choose"];
//self.tipAmountIndex = NSNotFound
// array to hold all tip values
if (self.tipAmountIndex != NSNotFound) {
NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)];
tipAmount = billAmount * [tipValues[self.tipControl.selectedSegmentIndex] floatValue];
totalAmount = tipAmount + billAmount;
}else {
if (intValue && billAmount > 0) {
tipAmount = .01 * intValue * billAmount;
totalAmount = tipAmount + billAmount;
//[self.tipControl setSelectedSegmentIndex:-1 ];
self.tipAmountIndex = NSNotFound;
}
}
self.tipLabel.text = [NSString stringWithFormat:@"$%0.2f", tipAmount];
self.totalLabel.text = [NSString stringWithFormat:@"$%0.2f", totalAmount];
}发布于 2013-12-15 21:19:58
您需要向分段控件添加一个目标/操作,以便它在分段更改时告诉您,您可以运行您的代码。假设您的代码在refreshForPercentageChange方法中
[self.segmentControl addTarget:self action:@selector(refreshForPercentageChange) forControlEvents:UIControlEventValueChanged];具体来说,对于更新后的代码,可以使用变量tipAmountIndex。当用户输入特定值时,将其设置为NSNotFound。如果线段被攻丝,则将其设置为索引。然后在您的代码中使用:
if (self. tipAmountIndex != NSNotFound) {但是这个if应该只决定抽头乘数。else应添加默认/用户设置的tip倍增器。然后计算在if之后(而不是内部)。
https://stackoverflow.com/questions/20594927
复制相似问题