stackiphone
stackiphone

Reputation: 1245

How to test if a UIButton title is nil

I have a twitterlogeout button when the user sucessfully logs in the username of the twitter will appear in the logoutbutton.my code for that is

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
    NSLog(@"Authenicated for %@", username);
 [_btntwitterLogeout setTitle:username forState:UIControlStateNormal];
}

but i want to set a condition if the _btntwitterLogeout.title = nil then ……. do some thing but i didnt know how to set condition if there is no title in the logout button. i put this code,but no luck

if (_btntwitterLogeout.titleLabel.text == nil) {
            _btntwitterLogeout.hidden = YES;
            _btnTwitter.hidden=NO;
        }
        else {
            _btntwitterLogeout.hidden = NO;
            _btnTwitter.hidden=YES;
        }

How can I do this?

Upvotes: 0

Views: 2288

Answers (3)

Sam
Sam

Reputation: 198

try this

if (![_btntwitterLogeout.titleLabel.text isKindOfClass:[NSNull class]]) {
    _btntwitterLogeout.hidden = YES;
    _btnTwitter.hidden=NO;
}
else {
    _btntwitterLogeout.hidden = NO;
    _btnTwitter.hidden=YES;
}

it's better approach rather checking length of Title string

Upvotes: 0

Apurv
Apurv

Reputation: 17186

Try this one:

if (btntwitterLogeout.titleLabel.text.length == 0)
{

    _btntwitterLogeout.hidden = YES;
    _btnTwitter.hidden=NO;
}
else
{

    _btntwitterLogeout.hidden = NO;
    _btnTwitter.hidden=YES;
}

Upvotes: 2

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Try this one....

if ([_btntwitterLogeout.titleLabel.text isEqualToString:@""]) {
    _btntwitterLogeout.hidden = YES;
    _btnTwitter.hidden=NO;
}
else {
    _btntwitterLogeout.hidden = NO;
    _btnTwitter.hidden=YES;
}

Upvotes: 1

Related Questions