简体   繁体   English

VC之间的数据传递:目标C

[英]Data Passing between VC : Objective C

I am newbie working on objective C. I am facing one problem . 我是从事目标C的新手。我面临一个问题。 I have tab bar controller containing three view controllers out of which I am concerned about only two VCs named "Setting" and "BBVC" . 我有一个包含三个视图控制器的选项卡栏控制器,我只关心其中两个名为“ Setting”和“ BBVC”的VC。 "BBVC" has a UIButton and "Setting" has a UISwitch (Please see below image). “ BBVC”具有UIButton,“设置”具有UISwitch(请参见下图)。 图片1

When button "B" is pressed, in tab bar view controller below code gets executed : 当按下按钮“ B”时,在标签栏视图控制器中,下面的代码将被执行:

- (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 BBVC作为弹出窗口加载

My aim is I want to change the value of "UISwitch" based on "UIButton" action event. 我的目标是我想基于“ UIButton”动作事件来更改“ UISwitch”的值。

Case 1 : Not On setting View 情况1:未设置视图
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 : 在这种情况下,按下UIButton后,当我使用“ Setting” VC时,可以通过使用viewWillappear和UserDefault来实现目标,如下所示:

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

}

Case 2 : 情况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. 在这种情况下,我已经在“ Settings” VC上(即已经加载了设置视图),并且当按下选项卡栏上的按钮“ B”时,它就会作为弹出窗口加载,如下图所示。 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. 尝试1:在设置VC中,我更新了“ viewDidAppear”方法中的代码,但是在调试时,我在关闭BBVC之后才知道,方法“ viewDidAppear”没有被调用。

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

}

Attempt 2 : 尝试2:

Use Delegate and Protocols : 使用代理和协议:

I have used delegate and protocols which is working fine but in this case address of UISwitch is nil. 我使用了可以正常工作的委托和协议,但是在这种情况下,UISwitch的地址为nil。 Please see below image 请看下图

图片3

Note : UISwitch is created programmatically. 注意:UISwitch是通过编程创建的。

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). 如果我正确地解释了您的问题,听起来您当前遇到的主要问题是在已经显示的情况下更新设置VC上的实时开关,但是BBVC会以模态方式显示在上方(并且已按下按钮)。

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 https://developer.apple.com/documentation/foundation/nsuserdefaultsdidchangenotification?language=objc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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