2012-12-15 82 views
1

我有一個正確交換圖像的Round Rect按鈕。我也想改變它們中的文字,但是文字沒有出現。 StackOverflow上的大多數問題似乎都涉及到一個問題,而不是另一個問題,而那些看起來最相關的問題最終會成爲某種類型的BarItem。如何更改iOS按鈕的圖像和文本?

從按鈕觸摸的IBAction中,我有以下幾點。

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

如上所述,圖像切換正常 - 但該標籤沒有顯示出來。我在另一個不交換圖像的按鈕中使用相同的文本交換線,它工作正常。

它只是在第一次點擊後不顯示。任何人都知道我能做些什麼來使文字顯示出來?

+0

你還在某處設置了屬性標題嗎? – Endophage

+0

正在做[sender setAttributedTitle:(@「What!」)];導致'NSInvalidArgumentException',原因:' - [UIAccessibilityBundle setAttributedTitle:]:無法識別的選擇器發送給實例'異常,恐怕。 – Danny

+0

它是'[UIButton setAttributedTitle:forState:]',與'setTitle'具有相同的簽名。只是因爲歸屬標題比標題更受歡迎而提出要求。 – Endophage

回答

1

這適用於我。

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

enter image description here

enter image description here

我也注意到,該標籤是最右邊,而不是死點。標籤是否可能出現在錯誤的地方?註釋掉setTitle會產生相同的結果,按鈕標題被圖像偏移。

如何將UIImageView轉換爲按鈕。注意:這是非常骯髒的,應該包裝成一個班。我離開了,但註釋掉了代碼,只需點擊一下即可。沒有顯示的是UIImageView在xib文件上拋出並設置爲userenabled = true。

#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 
+0

有趣。我的按鈕佔據了整個區域,所以如果有偏移將它從屏幕上移開,它的缺失是有意義的。將實驗,謝謝!另外,請考慮在交換之前和之後都有圖像。 – Danny

+0

我會嘗試的幾件事是設置邊緣偏移量[發件人setTitleEdgeInsets:UIEdgeInsetsMake(-10.0,10.0,0.0,0.0)];或者添加新的標籤並通過觸摸傳遞。我個人討厭自定義它們,通常只是使用UIImageView並添加手勢和狀態,因此我有更多的控制權。 – Kibitz503

+0

我實際上在使用UIImageView之前,使用了不同圖像的不交換按鈕。然而,當按下並隱藏它時,它將不再被按下 - 這就好像圖像在隱藏按鈕後優先。如果您有關於如何使用UIImageView的博客文章,請隨時鏈接,儘管我致力於解決我現在丟失的文本! – Danny