Ios 分段控制在抽头上激活
我有一个Ios 分段控制在抽头上激活,ios,Ios,我有一个分段控件,我想在点击其中一个分段时激活它。我的应用程序已经做了一些计算,所以我希望分段控件只在被点击时激活。我创建了一个if语句来激活它,但我不确定要为条件设置什么。不确定我是否正确使用这个应用程序,但我只是在这里和那里测试 if (/** on tap **/){ NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)]; tipAmount = billAmount * [tipValues[self.tipControl.selectedSe
分段控件
,我想在点击其中一个分段时激活它。我的应用程序已经做了一些计算,所以我希望分段控件只在被点击时激活。我创建了一个if语句
来激活它,但我不确定要为条件设置什么。不确定我是否正确使用这个应用程序,但我只是在这里和那里测试
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
更新的updateValue
- (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];
}
您需要向分段控件添加一个目标/操作,以便它告诉您分段何时更改,并且您可以运行代码。假设您的代码位于方法
refreshForPercentageChange
中:
[self.segmentControl addTarget:self action:@selector(refreshForPercentageChange) forControlEvents:UIControlEventValueChanged];
特别是对于更新的代码,可以使用变量
tipAmountIndex
。当用户输入特定值时,将其设置为NSNotFound
。如果该段被点击,则将其设置为索引。然后在代码中使用:
if (self. tipAmountIndex != NSNotFound) {
但是如果的话,这个
应该只是确定抽头乘数。else
应添加默认/用户设置的小费乘数。然后计算在之后(不在内部),如果
您是想对点击做出反应,还是只想知道选择了哪个段?抱歉,不清楚。我正在做一个小费计算器,3个部分是10/15/20%,所以我想让它做的是使用里面的值来计算百分比。因此,知道选择了哪个段,并且当所选段被更改时,您有一个调用的方法吗?@DavidRönnqvist实际上我想尝试对点击做出反应。如果我只想在点击时做出反应,我会对if语句做什么?这种方式可以工作,但它会覆盖我以前使用自定义小费金额的功能。这只是将小费金额设置为设定值并重新计算的触发器。如果需要,用户仍然可以输入一个特定的值(这将导致另一次重新计算)。我对iOS开发非常陌生,我更新了updateValues
函数。不确定我是否做得对。你能看一下吗?谢谢接近。我不确定intValue
扮演什么角色。在if
中,我没有任何计算,只有一个设置,然后是if
之后的计算(使用该设置作为小费百分比)。