1

我有一个正确换出图像的圆形矩形按钮。我还想更改其中的文本,但该文本没有显示。StackOverflow 上的大多数问题似乎只涉及一个问题,而不是另一个问题,而且看起来最相关的问题最终都是针对某种 BarItem 的。

在按钮触摸的 IBAction 中,我有以下内容。

 [sender setImage:[UIImage imageNamed:@"swappingCorrectly.png"] forState:UIControlStateNormal];
 [sender setTitle:(@"Aww, this doesn't show up!") forState:UIControlStateNormal];

如前所述,图像切换得很好——但该标签没有出现。我在另一个不交换图像的按钮中使用了相同的文本交换行,它工作正常。

它只是在第一次点击后不显示。任何人都知道我可以做些什么来显示该文本?

4

1 回答 1

1

这对我有用。

- (IBAction)buttonPressed:(id)sender {
    [sender setImage:[UIImage imageNamed:@"button.jpeg"] forState:UIControlStateNormal];
    [sender setTitle:(@"Aww") forState:UIControlStateNormal];
}

在此处输入图像描述

在此处输入图像描述

我确实注意到标签在最右边,而不是死点。标签是否可能在错误的位置呈现?注释掉 setTitle 会产生相同的结果,按钮标题被图像偏移。

如何将 UIImageView 变成一个按钮。注意:这很脏,应该包装到一个类中。我离开了,但注释掉了代码,只是为了做点什么。未显示的是在 xib 文件上抛出并设置为 userenabled = true 的 UIImageView。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonPressed:)];
    [[self fakeButton] addGestureRecognizer:tap];

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 25)];
    label.text = @"unpressed";
    label.backgroundColor = [UIColor clearColor];
    [[self fakeButton] addSubview:label];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonPressed:(id)sender {
//    UIGestureRecognizer *gesture = (UIGestureRecognizer*)sender;
//    UIImageView* temp = (UIImageView*)gesture.view;
//    for (UILabel *label in temp.subviews) {
//        label.text = @"pressed";
//    }
//    temp.image = [UIImage imageNamed:@"button.jpeg"];
//    
//    //if you want it to reset after a Delay
//    [self performSelector:@selector(buttonUnpressed:) withObject:temp afterDelay:.2];

}

-(void)buttonUnpressed:(UIImageView*)view
{

//    for (UILabel *label in view.subviews) {
//        label.text = @"unpressed";
//    }
//    view.image = [UIImage imageNamed:@"origButton.jpeg"];
//    [self performSelector:@selector(buttonUnpressed:) withObject:view afterDelay:.2];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];

    if (CGRectContainsPoint([[self fakeButton] frame], point))
    {
            //Do touch down state
        NSLog(@"touchStarted");
        [[self fakeButton] setImage:[UIImage imageNamed:@"button.jpeg"]];
        for (UILabel *label in [[self fakeButton] subviews]) {
            label.text = @"pressed";
        }
    }

}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch ended");
    //remove touch down state
    [[self fakeButton] setImage:[UIImage imageNamed:@"origButton.jpeg"]];
    for (UILabel *label in [[self fakeButton] subviews]) {
        label.text = @"unpressed";
    }

}

@end
于 2012-12-15T00:22:25.250 回答