1

I have declared a Button in my MainViewController so when a user logs in it needs to show a button using self.mybutton.hidden = FALSE; but on my ProfileViewController I have the logout button which needs to hide the button again using self.mybutton.hidden = TRUE;

Which is the best way to communicate between views?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
SirL0fty
  • 55
  • 1
  • 8
  • Properties or protocols are the best way of communicating between different controllers, but I can't say which one would work better for you considering that your question lacks details. – Adis Aug 28 '12 at 14:18

2 Answers2

0

NSUserDefaults could be an easy solution for you without too much thought. Save a particular BOOL value (if you insist) into that, then in each view check that value whether you should be hiding or showing that button:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:TRUE forKey:@"checkThisBool"];
Brayden
  • 1,795
  • 14
  • 20
  • 1
    While this will definitely do the trick, this is a misuse of the `NSUserDefaults` system. I would strongly recommend against following this pattern, even for a single `BOOL` flag. – Sergey Kalinichenko Aug 28 '12 at 14:25
  • I would disagree to your statement. As NSUserDefaults serve a primary use and function of storing application settings, it was built also to allow for small data state saving throughout applications. It really has nothing against it using it for this as it's just another key/value pair stored in a PLIST file. – Brayden Aug 28 '12 at 14:38
0

If you are using storyboard, you can pass any information under the prepareForSegue function.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:...]) { 
    MyViewController *controller = (MyViewController *segue.destinationViewController;
    controller.myProperty1 = ...; 
    controller.myProperty2 = ...;
  }
} 
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
TigerCoder
  • 93
  • 10