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

Posted

技术标签:

【中文标题】当按下自定义按钮时,UILabel 不会更新(Objective-C)【英文标题】:When a custom button is pressed UILabel won't update (Objective-C) 【发布时间】:2016-06-29 14:11:14 【问题描述】:

(我目前正在学习 ios 开发,因此在我知识有限的情况下,我深表歉意。)

我想要完成的是,每当您按下加法或减法按钮时,标签都会增加一,并按标签显示结果。我无法在标签中显示结果。

这是我的 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

【问题讨论】:

我没有看到与更新 valueLabel 文本相关的代码。 我看到两个“valueLabel”。我不确定哪一个应该真正改变。你没有设置StepperView对象的委托,它的委托方法switchViewValueChanged:没有被调用。设置文字时没有代码,只有NSLog() 【参考方案1】:

我不确定我是否清楚你想要做什么,但你为什么不尝试使用 iOS 提供的 UIStepper 按钮呢? 您可以将 IBAction 添加到 viewController 并使用 UIStepper 中的值更改标签。 使用快速:

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)"
   

【讨论】:

【参考方案2】:

目前还不是很清楚你想获得什么作为 UI,我正在发布一个可能的解决方案,它正在使用

    将信息从StepperView 传递给ViewController 的委托 UILabel 显示结果是ViewController's 视图的子视图,而不是StepperView 的子视图

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

这应该让您更清楚地了解不同组件应该如何相互对应。

【讨论】:

以上是关于当按下自定义按钮时,UILabel 不会更新(Objective-C)的主要内容,如果未能解决你的问题,请参考以下文章

使用 buttonPress 淡化 UIColor

按下按钮时未从数组中填充 UILabel

当按下另一个可触摸的不透明度时,一个可触摸的不透明度不会按下

XCTestCase:无法在导航栏中按下自定义栏按钮

检测何时按下自定义 UIButton

当按下返回按钮时,listview 会顶部。如何解决?