对于以下代码(UIButton未连接到IBOutlet的情况):
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];
在.h文件中声明了PlayButton指针。当我使用此代码时,按钮文本不会出现,但是按钮会显示图像。如何显示文字?
最佳答案
尝试这个 :
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];
我认为您忘了声明按钮的类型
关于objective-c - UIButton不显示使用[UIButton名称setTitle]设置的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7642908/