我有一个自定义类型LVSBBSettingsViewController
的VC用于用户设置。 VC由LVSMainViewController
中的主菜单显示。主VC以编程方式设置VC中的控件值。但是,当出现设置视图时,控件全部恢复为故事板中分配给它们的值。
我正在使用委托关闭设置视图,并在关闭时将数据从设置VC传回主VC。但是我不认为这是导致问题的原因,因为即使我删除它也会发生同样的事情。
造成这种情况的原因是什么?我有一种感觉,我错过了一些非常简单的东西......
LVSBBSettingsViewController.h:
#import <UIKit/UIKit.h>
@class LVSBBSettingsViewController;
#pragma mark LVSBBSettingsViewController Delegate
@protocol LVSBBSettingsViewControllerDelegate <NSObject>
- (void)settingsViewControllerDidCancel:(LVSBBSettingsViewController *)controller;
- (void)settingsViewControllerDidSave:(LVSBBSettingsViewController *)controller;
@end
#pragma mark LVSBBSettingsViewController
@interface LVSBBSettingsViewController : UITableViewController
@property (nonatomic, weak) id <LVSBBSettingsViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UISwitch *showBranchVarLabelsSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *useAnimationSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *showAllNodesSwitch;
@property (weak, nonatomic) IBOutlet UILabel *tempLabel;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
LVSBBSettingsViewController.m:
#import "LVSBBSettingsViewController.h"
@interface LVSBBSettingsViewController ()
@end
@implementation LVSBBSettingsViewController
// ... Xcode-generated stuff ...
- (IBAction)cancel:(id)sender
{
[self.delegate settingsViewControllerDidCancel:self];
}
- (IBAction)done:(id)sender
{
[self.delegate settingsViewControllerDidSave:self];
}
@end
LVSBBMainViewController.h:
#import <UIKit/UIKit.h>
#import "LVSBBSettingsViewController.h"
@interface LVSMainViewController : UIViewController <LVSBBSettingsViewControllerDelegate>
@end
LVSBBMainViewController.m:
#import "LVSMainViewController.h"
#import "LVSBBMasterViewController.h"
@interface LVSMainViewController ()
@end
@implementation LVSMainViewController
{
LVSBBMasterViewController *bbmvc;
}
// ...
- (void)viewDidLoad
{
[super viewDidLoad];
// Get main storyboard
UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];
// Instantiate bbmvc
bbmvc = [st instantiateViewControllerWithIdentifier:@"BBMasterViewControllerStoryboard"];
// Initialize settings
bbmvc.showBranchVarLabels = YES;
bbmvc.useAnimation = YES;
bbmvc.showAllNodes = NO;
}
...
#pragma mark LVSBBSettingsViewController Delegate
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ShowSettings"])
{
// Get pointer to settings VC
UINavigationController *navigationController = segue.destinationViewController;
LVSBBSettingsViewController *settingsViewController = [navigationController viewControllers][0];
// Set delegate
settingsViewController.delegate = self;
// Populate settings VC
// (same problem occurs if I replace right-hand sides of next 3 lines with NO;)
settingsViewController.showBranchVarLabelsSwitch.on = bbmvc.showBranchVarLabels;
settingsViewController.useAnimationSwitch.on = bbmvc.useAnimation;
settingsViewController.showAllNodesSwitch.on = bbmvc.showAllNodes;
settingsViewController.tempLabel.text = @"HELLO";
}
}
- (void)settingsViewControllerDidCancel:(LVSBBSettingsViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)settingsViewControllerDidSave:(LVSBBSettingsViewController *)controller
{
// Set settings in bbmvc
bbmvc.showBranchVarLabels = controller.showBranchVarLabelsSwitch.on;
bbmvc.useAnimation = controller.useAnimationSwitch.on;
bbmvc.showAllNodes = controller.showAllNodesSwitch.on;
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
更新:作为解决方法,我在LVSBBSettingsViewController
中添加了与LVSMainViewController
中的属性匹配的属性。在prepareForSegue:sender:
中,我设置了这些属性,而不是直接设置控件。然后在viewDidLoad
LVSBBSettingsViewController
中,我根据属性设置控件值。这似乎有效。但仍然不确定为什么我不能直接设置控制值。