I have three buttons with all same property other than their title. Titles are ‘A’, ‘B’ and ‘C’ respectively. I wrote a method where I have written all common properties of these three buttons and calling this method where I want to add those buttons on view. I have done all theses, but struct at button Title. I used for loop to add theses buttons to view.
Here is my code:
This is method with all common properties of three buttons.
-(UIButton *)setMiddlePlayButton{
UIButton * middleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[middleButton setBackgroundImage:[UIImage imageNamed:@"Middle"] forState:UIControlStateNormal];
[middleButton addTarget:self action:@selector(playButtonPressed:)
forControlEvents:UIControlEventTouchDown];
middleButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
return middleButton;
}
Calling the method in this way to add buttons on the view.
for(int i= 0; i<3; i++){
UIButton * middleButtons = [self setMiddlePlayButton];
middleButtons.tag= 2+i;
//Here I have to add button Title which is 'A' for first button with tag 2, 'B' for second button with tag 3 and 'C' for third button.
[self.view addSubView:middleButtons];
}
I am not finding a way to get the title, is anything possible with ASCII value? Or is there any other way to do this? Please help.
Try this code