У меня есть это 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];
}
Вы пытаетесь отреагировать на ответвление или просто хотите узнать, какой сегмент выбран? –
Жаль, что неясно. Я делаю калькулятор наконечника, а 3 сегмента - 10/15/20 процентов, поэтому я хочу, чтобы он использовал значения внутри для вычисления процентов. Знайте, какой сегмент выбран – Liondancer
И у вас есть метод, который вызывается при изменении выбранного сегмента? – Wain