首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分段式控制激活

分段式控制激活
EN

Stack Overflow用户
提问于 2013-12-15 21:13:38
回答 1查看 148关注 0票数 0

我有这个segmented control,当点击其中一个段时,我想让它激活一些东西。我的应用程序已经做了一些计算,所以我希望分段控件只在被点击时激活。我创建了一个if statement来激活它,但我不确定如何设置条件。不确定我是否正确地使用了这个应用程序,但我只是在测试一些东西。

代码语言:javascript
复制
if (/** on tap **/){

NSArray *tipValues = @[@(0.1), @(0.15), @(0.2)];
tipAmount = billAmount * [tipValues[self.tipControl.selectedSegmentIndex] floatValue];
totalAmount = tipAmount + billAmount;
}

以下是我的项目的一部分代码:

代码语言:javascript
复制
#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

代码语言:javascript
复制
- (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];

}
EN

回答 1

Stack Overflow用户

发布于 2013-12-15 21:19:58

您需要向分段控件添加一个目标/操作,以便它在分段更改时告诉您,您可以运行您的代码。假设您的代码在refreshForPercentageChange方法中

代码语言:javascript
复制
[self.segmentControl addTarget:self action:@selector(refreshForPercentageChange) forControlEvents:UIControlEventValueChanged];

具体来说,对于更新后的代码,可以使用变量tipAmountIndex。当用户输入特定值时,将其设置为NSNotFound。如果线段被攻丝,则将其设置为索引。然后在您的代码中使用:

代码语言:javascript
复制
if (self. tipAmountIndex != NSNotFound) {

但是这个if应该只决定抽头乘数。else应添加默认/用户设置的tip倍增器。然后计算在if之后(而不是内部)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20594927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档