gpt4 book ai didi

iphone - 为 UIControlStateNormal 添加目标不起作用

转载 作者:行者123 更新时间:2023-11-28 19:13:07 25 4
gpt4 key购买 nike

我创建了一个名为 SectionButton 的自定义 UIButton。

按钮有 2 张图片,一张用于正常状态,另一张用于选中和突出显示状态。

按钮也有文本,按下按钮时,必须调整 TitleEdgesInset。

在 init 方法中我添加了方法:

[self addTarget:self action: @selector(buttonHighlighted:) forControlEvents: UIControlStateHighlighted];
[self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlStateNormal];

但是从未调用方法“buttonNormal:”,所以我无法调整 titleEdgeInsets 属性。

#import "SectionButton.h"

@implementation SectionButton

- (id)initWithFrame: (CGRect)frame andTitle: (NSString*)title andbaseImageName:(NSString*)imageBaseName
{
if (self = [super initWithFrame: frame])
{
// Create images for button
UIImage* normalImage = [UIImage imageNamed: imageBaseName ];
UIImage* downImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageBaseName]];
// Set up button
[self setTitle: [title uppercaseString] forState: UIControlStateNormal]; // Will be used for all states
[self setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];

[self setBackgroundImage: normalImage forState: UIControlStateNormal];
[self setBackgroundImage: downImage forState: UIControlStateHighlighted];
[self setContentEdgeInsets:UIEdgeInsetsMake(54, 0, 0, 0)];

[self addTarget: self action: @selector(buttonHighlighted:) forControlEvents: UIControlStateHighlighted];
[self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlStateNormal];

}
return self;
}

- (void)buttonHighlighted: (id)sender
{
[self setTitleEdgeInsets:UIEdgeInsetsMake(0,0,-16,0)];
NSLog(@"button selected");
}

- (void) buttonNormal: (id)sender {
[self setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
NSLog(@"Button normal");
}

}
@end

最佳答案

您正在传递控制状态而不是 ControlEvent,

[self addTarget:self action: @selector(buttonHighlighted:) forControlEvents: UIControlEventTouchDownInside];
[self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlEventTouchUpInside];

关于iphone - 为 UIControlStateNormal 添加目标不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13913334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com