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

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

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

1) i have view controller file that has one button and i want change button title when didFinishedLaunching method called from app-delegate.

i also initialized the controller but that has no change.

thank in advance..

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Priyank Gandhi
  • 626
  • 2
  • 8
  • 21
  • possible duplicate of [is it possible to update UIButton title/text programmatically?](http://stackoverflow.com/questions/1033763/is-it-possible-to-update-uibutton-title-text-programmatically) – Anoop Vaidya May 19 '14 at 09:52
  • When are you calling the above code? Is loginButton nil? if it is I am guessing either it wasn't initialised in `self.loginViewController` or `self.loginViewController` itself is nil? Maybe you should log it out and update the question – liamnichols May 19 '14 at 10:11
  • i know how to do programmatically. but my question is i have one view controller that has one button and i want to change title from app delegate file. @AnoopVaidya – Priyank Gandhi May 19 '14 at 10:28
  • use the reference of that button or pass the title as string via a delegate – Anoop Vaidya May 19 '14 at 10:29

1 Answers1

1

To set the title correctly:

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


edit: If you are looking to update view states after a change from the AppDelegate, it would be a good idea to look at using NSNotifcationCenter. In the app delegate, you can post a notification about the user logging in or out, and then you can configure your viewController to be an observer for the notification and update its state when the notification is made.

For example, in your app delegate

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

Then in your 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];
}
liamnichols
  • 12,419
  • 2
  • 43
  • 62
  • yeahh...this is right but also check loginButton whether it is initialized or not as it is returning nil. – Prashant Nikam May 19 '14 at 10:01
  • Please provide a little bit of explanation. Code only answers are not appreciated and are automatically flagged for admin approval – GôTô May 19 '14 at 10:10
  • sorry.. i've added a comment to the original question now as it is hard to determine why the titleLabel text is nil and not an empty string. – liamnichols May 19 '14 at 10:15
  • i do initialized but it's not any change. @liamnichols – Priyank Gandhi May 19 '14 at 10:36
  • i do this code but it's not working for me. @liamnichols – Priyank Gandhi May 19 '14 at 11:18
  • after re-reading your question, it probably isn't suitable... What sort of check are you running in didFinishedLaunching? Maybe you should move the code you rely on into some sort of shared singleton instance to run your login checks, that way you can call the appropriate method directly from inside the view controller – liamnichols May 19 '14 at 12:07
  • If you want use `NSNotificationCenter` I recommend this library https://github.com/AllinMobile/AIMObservers – Maciej Gad Dec 12 '15 at 12:13