2011-10-04 66 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