Thursday, May 30, 2013

UIButton setTitle example in Objective C (iOS).

UIButton setTitle

Sets the title to use for the specified state.

- (void)setTitle:(NSString *)title forState:(UIControlState)state

Parameters
title
The title to use for the specified state.
state
The state that uses the specified title. The possible values are described in UIControlState.

Discussion of [UIButton setTitle]
Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.[UIButton setTitle]

At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.

UIButton setTitle example.
UIButton *backbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 68, 38)];
[backbtn setTitle:@"Your string" forState:UIControlStateNormal];
//[backbtn setImage:[UIImage imageNamed:@"BackBtn.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backbtn];

Example of [UIButton setTitle].
MyViewcontroller *myView = [[MyViewcontroller alloc] initWithNibName:@"MyViewcontroller" bundle:nil];
[scrollView addSubview:myView.view]; // view is loaded
[myView.imageButton setTitle:@"ddd" forState:UIControlStateNormal]; // imageButton is now wired

UIButton setTitle example.
UIButton *dateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dateButton setFrame:CGRectMake(240.0, 57.0, 60.0, 30.0)];
[dateButton setTitle:@"Date" forState:UIControlStateNormal];

[dateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dateButton setBackgroundImage:[UIImage imageNamed:@"bg_headline.png"] forState:UIControlStateNormal];
[dateButton addTarget:self action:@selector(GoToDateSettings:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dateButton];

End of UIButton setTitle example article.