尝试访问 parentViewController 中的方法时,我的应用程序崩溃。这是 StoryBoard 中的布局
MainViewController = STLMMainViewController (ParentViewController)
Scene1 = STLMTimeDateViewController (ChildViewController)
这是 STLMTimeDateViewController 的代码
#import "STLMTimeDateViewController.h"
#import "STLMMainViewController.h"
@interface STLMTimeDateViewController ()
@property (nonatomic, strong) STLMMainViewController *stlmMainViewController;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"The name of the controller %@",self.navigationController.parentViewController);
stlmMainViewController= (STLMMainViewController *) self.parentViewController;
[stlmMainViewController locationButtonSelected]; // This is where the App crashes
NSLog(@"TimeDateController");
}
应用程序运行,但调用 STLMMainViewController 时,应用程序崩溃并出现以下错误:
2013-02-10 16:33:57.422 MyApp[9120:c07] The name of the controller <STLMMainViewController: 0x83850d0>
2013-02-10 16:33:57.434 MyApp[9120:c07] -[UINavigationController locationButtonSelected]: unrecognized selector sent to instance 0x8371a70
如果我删除以下行:
stlmMainViewController = (STLMMainViewController *) self.parentViewController;
然后离开
[stlmMainViewController locationButtonSelected];
应用程序运行,没有错误,但 [STLMMainViewController locationButtonSelected] 中的以下方法没有被调用(我从未看到日志):
-(void)locationButtonSelected
{
[LocationButton setSelected:YES];
[eatDrinkbutton setSelected:NO];
[timeCalButton setSelected:NO];
[carButton setSelected:NO];
[contactButton setSelected:NO];
NSLog(@"LocationButtonSelected Method");
}
locationButtonSelected 方法中的所有属性和方法本身都在 STLMMainViewController 的 .h 中声明,以供公共访问。
谢谢