如何为UIButton设置标题?

时间:2011-11-22 12:43:09

标签: iphone

我们如何设置按钮的按钮标题,我知道这个答案很简单,我们需要设置标题按钮的标题,但我的需要有点不同,我有一个用户名,根据登录动态更改.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];    
}

我需要在另一个按钮单击中显示用户名,并且必须将该按钮的标题分配给用户名,代码为

- (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;
    }   
}

我需要在[_loginButton setTitle:@"" forState:UIControlStateNormal];我想要[_loginButton setTitle:username forState:UIControlStateNormal];

中设置用户名

2 个答案:

答案 0 :(得分:0)

无论如何,你已经在方法

中设置了按钮标题
- (void)getFacebookProfileFinished:(ASIHTTPRequest *)request
在facebook请求完成后

。那么,为什么你需要在refresh方法中再次设置它?只需在LoginStateLoggedIn方法的refresh条件中注释掉该行。

//[_loginButton setTitle:@"" forState:UIControlStateNormal];

修改

- (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;
    }   
}

答案 1 :(得分:0)

您需要的是在两个代码都可以访问的位置存储对该消息的引用。这样做的典型方法是在接口定义中添加NSString * _username,并在收到时将其设置为用户名。这样,您始终可以从任何取决于其当前值的位置访问它。

包括内存管理,这将产生:

[_username release]; // Release possible previous login
_username = [username retain]; // Ownership needed

刷新时:

[_loginButton setTitle:_username forState:UIControlStateNormal];

在dealloc中:

[_username release]; // Release ownership when not needed anymore

这允许您在任何所需的位置访问您的变量。