2011-10-04 63 views
1

對於以下代碼(其中的UIButton未連接到一個IBOutlet)設置:的UIButton不diplay文本與[UIButtonname的setTitle]

UIImage *defaultButton = [UIImage imageNamed:@"StandardButton.png"]; 
PlayButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 50, 220, 50)]; 
[PlayButton setImage:defaultButton forState:UIControlStateNormal]; 
[PlayButton setTitle:@"PlayButton" forState:UIControlStateNormal]; 
[PlayButton addTarget:self action:@selector(StoryModeReleased:)forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:PlayButton]; 

的PLAYBUTTON指針在.h文件聲明。當我使用此代碼時,按鈕文本不會顯示,但按鈕會顯示圖像。我怎樣才能讓文字顯示?

回答

2

試試這個:

PlayButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
UIImage *defaultButton = [UIImage imageNamed:@"StandardButton.png"]; 
PlayButton.frame = CGRectMake(250, 50, 220, 50)]; 
[PlayButton setImage:defaultButton forState:UIControlStateNormal]; 
[PlayButton setTitle:@"PlayButton" forState:UIControlStateNormal]; 
[PlayButton addTarget:self action:@selector(StoryModeReleased:)forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:PlayButton]; 

我想你忘記了聲明按鈕的類型

+0

你怎麼聲明的類型? – rdelfin

+0

我宣佈的類型,如果我使用圖像(我使用的服裝類型)我不能設置標題 – rdelfin

+1

我找到答案(供將來參考)。您必須設置backgroundImage,而不是圖像 – rdelfin