rdelfin
rdelfin

Reputation: 816

UIButton does not diplay text set with [UIButtonname setTitle]

For the following code (where the UIButton is not connected to an 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];

The PlayButton pointer is declared in the .h file. The button text does not appear when I use this code, but the button displays whith the image. How can I make the text display?

Upvotes: 1

Views: 741

Answers (1)

Alex Moskalev
Alex Moskalev

Reputation: 607

Try this :

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];

I think you forgot about declare the type of the button

Upvotes: 2

Related Questions