Reputation: 16469
I have this segmented control
and I want it to activate something when one of the segments is tapped. My app already does some computation so I want the segmented control to activate only upon being tapped. I created an if statement
to activate it but I'm not sure what to put for the conditions. Not sure if I am going about this app properly but Im just testing things here and there.
if (/** on tap **/){
NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)];
tipAmount = billAmount * [tipValues[self.tipControl.selectedSegmentIndex] floatValue];
totalAmount = tipAmount + billAmount;
}
Here is the code for part of my project:
#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
Updated 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];
}
Upvotes: 0
Views: 155
Reputation: 119031
You need to add a target / action to the segmented control so that it tells you when the segment is changed and you can run your code. Assuming your code is in the method refreshForPercentageChange
:
[self.segmentControl addTarget:self action:@selector(refreshForPercentageChange) forControlEvents:UIControlEventValueChanged];
Specifically for your updated code, you could have a variable tipAmountIndex
. When the user enters a specific value, set it to NSNotFound
. If the segment is tapped, set it to the index. Then in your code you would use:
if (self. tipAmountIndex != NSNotFound) {
but this if
should be around determining the tap multiplier only. The else
should add the default / user set tip multiplier. Then the calculation is after (not inside) the if
.
Upvotes: 2