如何更改设置标题uibutton?

时间:2014-05-19 09:48:42

标签: ios7 uibutton storyboard

UIButton *loginButton = [self.loginViewController LoginButton];
loginButton.titleLabel.text=@"Log out";

//[loginButton setTitle:@"Log out" forState:UIControlStateNormal];

 NSLog(@"Log in :-%@",loginButton.titleLabel.text);

1)我有一个视图控制器文件,有一个按钮,当从app-delegate调用didFinishedLaunching方法时我想要更改按钮标题。

我也初始化了控制器,但没有变化。

提前谢谢..

1 个答案:

答案 0 :(得分:1)

正确设置标题:

[loginButton setTitle:@"Log Out" forState:UIControlStateNormal];


编辑:如果您希望在从AppDelegate进行更改后更新视图状态,最好使用NSNotifcationCenter进行查看。在app delegate中,您可以发布有关用户登录或注销的通知,然后您可以将viewController配置为通知的观察者并在发出通知时更新其状态。

例如,在您的app delegate

- (void)userDidLogOut
{
    //This method would be called when you logout
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didLogoutNotification" object:nil];
}

然后在你的loginViewController

- (void)viewDidLoad
{
    //...

    //Become an observer of `didLogoutNotification`.
    [[NSNoficationCenter defaultCenter] addObserver:self selector:@selector(didLogoutNotification:) name:@"didLogoutNotification" object:nil];
}

- (void)dealloc
{
    //...

    //Remove yourself from the observation list.
    [[NSNoficationCenter defaultCenter] removeObserver:self];
}

- (void)didLogoutNotification:(NSNotification *)notification
{
    //...

    //Update the button
    [loginButton setTitle:@"Log In" forState:UIControlStateNormal];
}