按下自定义按钮时UILabel将不会更新(Objective-C)

时间:2016-06-29 14:11:14

标签: ios objective-c uiview uibutton uilabel

(我目前正在学习iOS开发,所以我对我的有限知识进行了道歉。)

我想要完成的是,无论何时按下加法或减法按钮,标签都会递增1并按标签显示结果。我在标签中显示结果时遇到了困难。

这是我的ViewController.h文件:

#import <UIKit/UIKit.h>
#import "StepperView.h"

@interface ViewController : UIViewController{
    UILabel *valueLabel;
}

@end

这是我的ViewController.m文件:

#import "ViewController.h"
#import "StepperView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    int w;

    w = 0;
    [super viewDidLoad];

    StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];

    stepperView.center = self.view.center;
    [self.view addSubview:stepperView];

    //    AddButton
    UIButton *additionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
    additionButton.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 25, 100, 50);
    [self.view addSubview:additionButton];

    //    Subtraction button
    UIButton *subtractionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
    subtractionButton.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 25, 100, 50);
    [self.view addSubview:subtractionButton];

    //    Label
    valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
    [valueLabel setTextColor:[UIColor redColor]];

    [self.view addSubview:valueLabel];
}

@end

这是我的代表和议定书:

StepperView.h

#import <UIKit/UIKit.h>

@protocol SwitchViewDelegate

- (void)switchViewValueChanged:(int)number;

@end

@interface StepperView : UIView
{


}

@property (nonatomic,weak) id<SwitchViewDelegate> delegate;

@end

这是我的StepperView.m文件

#import "StepperView.h"

@implementation StepperView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self setup];
    return self;
}

- (void)setup {
    UIView *stepperView = [[UIView alloc] init];
    stepperView.frame = CGRectMake(0, 0, 300, 40);

    stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
    [self addSubview:stepperView];

    //    Add Button
    UIButton *rightView = [[UIButton alloc] init];
    rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);

    rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
    [rightView setTitle:@"+" forState:UIControlStateNormal];

    [rightView addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:rightView];        

    //    Subtract Button
    UIButton *leftView = [[UIButton alloc] init];
    leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);

    leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
    [leftView setTitle:@"-" forState:UIControlStateNormal];
    [leftView addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftView];        

    //    Label

 UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(stepperView.frame.size.width/2, stepperView.frame.size.height/2, 100, 100)];

    [valueLabel setText:@"10"];
    [valueLabel setTextColor:[UIColor redColor]];
    [self addSubview:valueLabel];        
}

- (void)buttonPressed:(id)sender{        
    UIButton *button = (UIButton *) sender;

    NSString *title = button.titleLabel.text;
    if([title isEqualToString:@"-"]) {                        
        NSLog(@"MINUS");
    } else {
        NSLog(@"PLUS");
    }

    //    NSLog(@"%@",title);
   }

@end

2 个答案:

答案 0 :(得分:0)

目前还不是很清楚你在尝试获取的UI是什么,我发布了一个可能的解决方案,正在使用

  1. 代表将信息从StepperView传递到ViewController
  2. UILabel显示的结果是ViewController's视图的子视图,而不是StepperView
  3. 的子视图

    ViewController.h:

    #import <UIKit/UIKit.h>
    #import "StepperView.h"
    
    @interface ViewController : UIViewController
        @property (nonatomic, strong) UILabel *valueLabel;
    @end
    

    ViewController.m

    #import "ViewController.h"
    #import "StepperView.h"
    
    @interface ViewController () <StepperViewDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
    
        stepperView.center = self.view.center;
        [self.view addSubview:stepperView];
        stepperView.delegate = self;
    
        //    Label
        self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
        [self.valueLabel setTextColor:[UIColor redColor]];
    
        [self.view addSubview:self.
    
    valueLabel];
        }
    
    #pragma mark - StepperViewDelegate
    
    - (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value {
    
    self.valueLabel.text = [NSString stringWithFormat:@"%d", value];
    }
        @end
    

    StepperView.h

    #import <UIKit/UIKit.h>
    @class StepperView
    @protocol StepperViewDelegate
      - (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value 
    @end
    @interface StepperView : UIView
    
    @property (nonatomic, weak) id<StepperViewDelegate> delegate;
    
    @property (nonatomic, assign) NSInteger stepperValue;
    @end
    

    StepperView.m

    #import "StepperView.h"
    
    @implementation StepperView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        [self setup];
        return self;
    }
    
    - (void)setup {
        UIView *stepperView = [[UIView alloc] init];
        stepperView.frame = CGRectMake(0, 0, 300, 40);
    
        stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
        [self addSubview:stepperView];
    
        //    Add Button
        UIButton *rightView = [[UIButton alloc] init];
        rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
    
        rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
        [rightView setTitle:@"+" forState:UIControlStateNormal];
    
        [rightView addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
        [self addSubview:rightView];        
    
        //    Subtract Button
        UIButton *leftView = [[UIButton alloc] init];
        leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
    
        leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
        [leftView setTitle:@"-" forState:UIControlStateNormal];
        [leftView addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    - (void)buttonPressed:(id)sender{        
        UIButton *button = (UIButton *) sender;
    
        NSString *title = button.titleLabel.text;
        if([title isEqualToString:@"-"]) {                        
            self.stepperValue--;
        } else {
            self.stepperValue++;
        }
    
     [self.delegate stepperView:self valueChanged:self.stepperValue];
        //    NSLog(@"%@",title);
       }
    
    @end
    

    这应该让你更清晰地了解不同组成部分应该如何相互对应。

答案 1 :(得分:0)

我不确定我是否理解您想要做什么,但为什么不尝试使用iOS提供的UIStepper按钮? 您可以向viewController添加IBAction,并使用UIStepper中的值更改标签。 使用swift:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var valueLabel: UILabel!
var currentValue = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func touchStepper(sender: UIStepper) {
    self.valueLabel.text = "\(sender.value)"
}   

}