[英]How to set title for UIButton?
How can we set the button title for a button,i know this answer its simple,we need to set the title of the button for title ,but my need is somewhat diffrent ,i had a username which dynamically changes according to login.i set this in a button click and display it in a label within that button .my code for this is 我们如何才能为按钮设置按钮标题,我知道这个答案很简单,我们需要为标题设置按钮的标题,但是我的需求有所不同,我有一个用户名,该用户名会根据login.i进行动态更改。单击此按钮,然后将其显示在该按钮内的标签中。我的代码是
- (void)getFacebookProfileFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(@"Got Facebook Profile: %@", responseString);
NSString *likesString;
NSMutableDictionary *responseJSON = [responseString JSONValue];
NSString *username;
NSString *firstName = [responseJSON objectForKey:@"first_name"];
NSString *lastName = [responseJSON objectForKey:@"last_name"];
if (firstName && lastName) {
username = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
} else {
username = @"mysterious user";
}
[_loginButton setTitle:username forState:UIControlStateNormal];
_lblfaceusername.text = username;
[self refresh];
}
i need to display the user name in another button click and have to assing the title of that button to user name the code for this is 我需要在另一个按钮单击中显示用户名,并且必须通过单击该按钮的标题来为用户名指定代码
- (void)refresh {
if (_loginState == LoginStateStartup || _loginState == LoginStateLoggedOut) {
_loginStatusLabel.text = @"Not connected to Facebook";
[_loginButton setTitle:@"Login" forState:UIControlStateNormal];
_loginButton.hidden = NO;
} else if (_loginState == LoginStateLoggingIn) {
_loginStatusLabel.text = @"Connecting to Facebook...";
_loginButton.hidden = YES;
} else if (_loginState == LoginStateLoggedIn) {
_loginStatusLabel.text = @"Connected to Facebook";
[_loginButton setTitle:@"" forState:UIControlStateNormal];
_loginButton.hidden = NO;
}
}
i need to set the username in the [_loginButton setTitle:@"" forState:UIControlStateNormal];
我需要在
[_loginButton setTitle:@"" forState:UIControlStateNormal];
设置用户名[_loginButton setTitle:@"" forState:UIControlStateNormal];
i want [_loginButton setTitle:username forState:UIControlStateNormal]
; 我想要
[_loginButton setTitle:username forState:UIControlStateNormal]
;
Anyhow you have set the button title in the method 无论如何,您已经在方法中设置了按钮标题
- (void)getFacebookProfileFinished:(ASIHTTPRequest *)request
after facebook request done. 在Facebook请求完成后。 Then, why you need to set that again in
refresh
method? 然后,为什么需要在
refresh
方法中再次进行设置? Just comment out the line in LoginStateLoggedIn
condition in refresh
method. 只需在
refresh
方法中LoginStateLoggedIn
条件中的行LoginStateLoggedIn
。
//[_loginButton setTitle:@"" forState:UIControlStateNormal];
Edit 编辑
- (void)refresh {
if (_loginState == LoginStateStartup || _loginState == LoginStateLoggedOut) {
_loginStatusLabel.text = @"Not connected to Facebook";
[_loginButton setTitle:@"Login" forState:UIControlStateNormal];
_loginButton.hidden = NO;
} else if (_loginState == LoginStateLoggingIn) {
_loginStatusLabel.text = @"Connecting to Facebook...";
_loginButton.hidden = YES;
} else if (_loginState == LoginStateLoggedIn) {
_loginStatusLabel.text = @"Connected to Facebook";
NSString *username = [_loginButton titleForState:UIControlStateNormal];
[_loginButton setTitle:username forState:UIControlStateNormal];
_loginButton.hidden = NO;
}
}
What you need is storing a reference to that message at a location that both pieces of code can access. 您需要将对消息的引用存储在两个代码段都可以访问的位置。 The typical way to do so is to add a NSString * _username in your interface definition, and setting it to the username when you receive it.
这样做的典型方法是在接口定义中添加NSString * _username,并在收到它时将其设置为用户名。 That way you can always access it from any location that depends on the current value of it.
这样,您始终可以从依赖于当前值的任何位置访问它。
Memory management included, that would yield: 包括内存管理,将产生:
[_username release]; // Release possible previous login
_username = [username retain]; // Ownership needed
In refresh: 刷新:
[_loginButton setTitle:_username forState:UIControlStateNormal];
In dealloc: 在dealloc中:
[_username release]; // Release ownership when not needed anymore
This allows you to access your variable at any location desired. 这使您可以在所需的任何位置访问变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.