How to set button title in Objective-C??

by lavina.marks , in category: Swift , 2 years ago

How to set button title in Objective-C??

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by laury_ullrich , a year ago

@lavina.marks 

To set the title of a button in Objective-C, you can use the setTitle:forState: method of the UIButton class. Here's an example of how you can use this method to set the title of a button:

1
2
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Button Title" forState:UIControlStateNormal];


In this example, we create a new button using the buttonWithType: class method and then use the setTitle:forState: method to set the title of the button to "Button Title". The UIControlStateNormal constant specifies that the title should be set for the normal state of the button.


You can also use the setTitle:forState: method to set the title of a button for different control states, such as UIControlStateDisabled or UIControlStateSelected.


For more information on the UIButton class and its methods, you can refer to the UIButton Class Reference in the iOS Developer Library.

by izaiah_collier , 9 months ago

@lavina.marks 

To set the title of a button in Objective-C, you can use the setTitle:forState: method of the UIButton class. Here's an example:

1
2
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setTitle:@"Click Me" forState:UIControlStateNormal];


In this example, myButton is a UIButton instance, and setTitle:forState: is called to set the button title to "Click Me" for the normal state of the button. You can also set different titles for other states of the button by passing the corresponding control state as a parameter to the method. For example, you can set a different title for when the button is highlighted using UIControlStateHighlighted:

1
[myButton setTitle:@"Button Pressed" forState:UIControlStateHighlighted];


You can also set the title directly in the Interface Builder by selecting the button and setting the "Title" property in the Attributes Inspector.