我正在构建一个调用不同子视图的应用,并将它们放在主视图控制器中(http://imgur.com/p6l9Oac)
当用户点击屏幕底部的按钮时(让我们称之为滑动菜单!)后面的视图将会消失,新的视图会显示出来。
其中一个子视图是“设置”,它有一些开关用于启用/禁用某些按钮。
在SettingsViewController中。 我设定了一个协议:
@protocol SettingsViewControllerDelegate <NSObject>
@required
-(void)hideCountdownView;
-(void)showCountdownView;
@end
,界面包含:
@interface SettingsHubViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISwitch *enableCountdown;
@property (nonatomic, assign) id <SettingsViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@end
并在实施中:
- (IBAction)switchAction:(id)sender {
if (!self.enableCountdown.on) {
NSLog(@"The view is Hidden");
[_delegate hideCountdownView];
} else if (self.enableCountdown.on) {
NSLog(@"The view is Shown");
[_delegate showCountdownView];
}
}
你可以看到我用_delegate来使用show和hide函数,我使用NSLog来确保我正确调用函数。
MainViewController中的
#import "SettingsHubViewController.h"
@interface MainViewController () <SettingsViewControllerDelegate>
@property (nonatomic, strong) SettingsHubViewController * settingsViewController;
和实施
@implementation MainViewController
-(void)showCountdownView {
self.slidingMainMenuViewController.countdownView.hidden = NO;
NSLog(@"Showing Countdown");
}
-(void)hideCountdownView {
self.slidingMainMenuViewController.countdownView.hidden = YES;
NSLog(@"Hiding Countdown");
}
-(void)viewDidLoad {
[super viewDidLoad];
self.settingsViewController.delegate = self;
self.slidingMainMenuViewController.delegate = self;
}
问题是上面的NSLogs根本没有被调用,任何人都可以帮助我吗?
由于
更新:由于我有20多个不同的视图需要调用,我创建了这个方法
- (UIView *) getPresentedMenu:(NSString *) menuIdentifer withMenuTag:(int) menuTag withAViewController:(UIViewController*) menuViewController andMenuDelegate:(id) menuDelegate {
menuViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:Nil] instantiateViewControllerWithIdentifier:menuIdentifer];
menuViewController.view.tag = menuTag;
if (self.viewBeingCalledBySwipe == NO) {
menuViewController.view.frame = CGRectMake(0, menuViewController.view.frame.size.height, menuViewController.view.frame.size.width, menuViewController.view.frame.size.height);
} else if (self.isItRightSwipe == YES) {
menuViewController.view.frame = CGRectMake(-menuViewController.view.frame.size.width, 0, menuViewController.view.frame.size.width, menuViewController.view.frame.size.height);
} else if (self.isItRightSwipe == NO) {
menuViewController.view.frame = CGRectMake(menuViewController.view.frame.size.width, 0, menuViewController.view.frame.size.width, menuViewController.view.frame.size.height);
}
[self.view addSubview:menuViewController.view];
[self addChildViewController:menuViewController];
[menuViewController didMoveToParentViewController:self];
UIView *view = menuViewController.view;
return view;
}
因此,当我需要某个视图控制器时,我只需要调用此函数
self.childView = [self getPresentedMenu:@"Settings" withMenuTag:SETTINGS_TAG withAViewController:self.settingsViewController andMenuDelegate:self.settingsViewController.delegate];
但此方法未分配代理
答案 0 :(得分:0)
如果settingsViewController是IBOutlet,则必须在其声明中添加IBOutlet
并使用storyboard或xib连接它。
@property (nonatomic, strong) IBOutlet SettingsHubViewController * settingsViewController;
但如果它不是Outlet,你必须在设置它的委托之前分配它。
self.settingsViewController = [[SettingsHubViewController alloc] init];
self.settingsViewController.delegate = self;
您还应该验证您的- (IBAction)switchAction:(id)sender
IBAction是否已在故事板或xib中正确连接。
答案 1 :(得分:0)
初始化SettingsViewController
时,请确保使用故事板方法而不是[[SettingsViewController alloc] init]
进行初始化。
首先在故事板视图中直接设置Storyboard ID
:
然后将其用于settingsViewController
:
self.settingsViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:NULL] instantiateViewControllerWithIdentifier:@"SettingsViewController"];
将MainStoryboard_iPhone
替换为您的故事板名称。
希望有所帮助!
答案 2 :(得分:0)
//如果您使用的是View Controller
SettingsHubViewController * settingVC = [[UIStoryboard storyboardWithName:@“ Main”包:无] InstantiateViewControllerWithIdentifier:@“ yourStoryBoardIdentifier”];
settingVC.delegate = self;
//如果您正在使用XIB,则在设置setting.VC.delegate = self;之后加载NIB。
它为我工作(我创建了OverLay视图并加载到主类中)