简体   繁体   中英

Data Passing between VC : Objective C

I am newbie working on objective C. I am facing one problem . I have tab bar controller containing three view controllers out of which I am concerned about only two VCs named "Setting" and "BBVC" . "BBVC" has a UIButton and "Setting" has a UISwitch (Please see below image). 图片1

When button "B" is pressed, in tab bar view controller below code gets executed :

- (void)centerButtonTapped:(id __unused)sender {
    BBVC *vc = [[BBVC alloc] init];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nc animated:YES completion:nil];
}

BBVC gets loaded as pop UP

My aim is I want to change the value of "UISwitch" based on "UIButton" action event.

Case 1 : Not On setting View
In this case after pressing the UIButton, when I am on "Setting" VC, the aim can be achieved by using viewWillappear and UserDefault as shown below :

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"viewWillAppear");
    [super viewWillAppear:animated];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [Switch setOn:[defaults boolForKey:@"EnableLIVE"] animated:YES];

}

Case 2 :

In this case I am already on "Settings" VC (ie setting view is already loaded) and when button "B" from tab bar is pressed, it gets loaded as a pop up as shown in below image. I am trying to achieve my aim but its not working.

图片2

Attempt 1 : In Setting VC, I updated the code in "viewDidAppear" method but while debugging I got to know after closing BBVC, method "viewDidAppear" is not getting called.

-(void)viewDidDisappear:(BOOL)animated
{
       NSLog(@"viewDidDisappear");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     [Switch setOn:[defaults boolForKey:@"EnableLIVE"] animated:YES];

}

Attempt 2 :

Use Delegate and Protocols :

I have used delegate and protocols which is working fine but in this case address of UISwitch is nil. Please see below image

图片3

Note : UISwitch is created programmatically.

I am clueless here. Any kind of help is appreciated. Thank you.

If i'm interpreting your question correctly, it sounds like the main problem you're experiencing currently is updating the live switch on the settings VC when it's already displaying, but the BBVC is displaying modally overtop (and it's button is pressed).

You can listen for a notification of changes to user defaults within your settings controller when it loads up, and remove it as an observer once deallocated -- then adjust the switch to the appropriate value once the notification of user defaults changing comes in. Something along these lines:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.liveSwitch setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"EnableLIVE"]];
}

- (void)userDefaultsDidChange:(NSNotification *)notification {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.liveSwitch setOn:[[notification object] boolForKey:@"EnableLIVE"]];
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

https://developer.apple.com/documentation/foundation/nsuserdefaultsdidchangenotification?language=objc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM