分段控制激活

时间:2013-12-15 13:13:38

标签: ios

我有这个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];

}

1 个答案:

答案 0 :(得分:2)

您需要向分段控件添加目标/操作,以便它告诉您何时更改分段并运行代码。假设您的代码位于方法refreshForPercentageChange

[self.segmentControl addTarget:self action:@selector(refreshForPercentageChange) forControlEvents:UIControlEventValueChanged];

专门针对您的更新代码,您可以拥有变量tipAmountIndex。当用户输入特定值时,将其设置为NSNotFound。如果点击该段,请将其设置为索引。然后在您的代码中使用:

if (self. tipAmountIndex != NSNotFound) {

但是这个if应该仅限于确定抽头乘数。 else应添加默认/用户设置提示倍数。然后计算在if之后(不在内部)。