我有一个多个XIB文件项目。我有一个switchViewController类来处理内容视图的切换。我想从其他类调用此类中的方法。我有以下代码:
//SwitchViewController.h
Code:
#import <UIKit/UIKit.h>
@class MainMenuViewController;
@class GlassRepairsViewController;
@interface SwitchViewController : UIViewController {
MainMenuViewController *mainMenuViewController;
GlassRepairsViewController *glassRepairsViewController;
}
@property (retain,nonatomic) MainMenuViewController *mainMenuViewController;
@property (retain,nonatomic) GlassRepairsViewController *glassRepairsViewController;
-(IBAction)goToGlassRepairs;
-(IBAction)goToMainMenu;
@end
//switch view controller m file
Code:
#import "SwitchViewController.h"
#import "MainMenuViewController.h"
#import "GlassRepairsViewController.h"
@implementation SwitchViewController
@synthesize MainMenuViewController;
@synthesize GlassRepairsViewController;
- (void)viewDidLoad {
MainMenuViewController *mainMenuController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil];
self.mainMenuViewController = mainMenuController;
[self.view insertSubview:mainMenuController.view atIndex:0];
[mainMenuController release];
[super viewDidLoad];
}
//These IBActions are linked to buttons on the view that is linked to this class (swtichViewController) This works perfectly for showing different nibs
-(IBAction) goToGlassRepairs
{
if(self.glassRepairsViewController.view.superview == nil)
{
if(self.glassRepairsViewController == nil)
{
GlassRepairsViewController *glassRepairsController = [[GlassRepairsViewController alloc] initWithNibName:@"GlassRepairsView" bundle:nil];
self.GlassRepairsViewController =glassRepairsController;
[glassRepairsController release];
}
[mainMenuViewController.view removeFromSuperview];
[self.view insertSubview:glassRepairsViewController.view atIndex:0];
}
}
-(IBAction) goToMainMenu
{
if(self.mainMenuViewController.view.superview == nil)
{
if(self.mainMenuViewController == nil)
{
MainMenuViewController *mainMenuController = [[ MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil];
self.mainMenuViewController = mainMenuController;
[mainMenuController release];
}
[glassRepairsViewController.view removeFromSuperview];
[self.view insertSubview:mainMenuViewController.view atIndex:0];
}
}
//MainMenuViewController.h
Code:
#import <UIKit/UIKit.h>
@class SwitchViewController;
@interface MainMenuViewController : UIViewController {
}
-(IBAction)goToGlass;
@end
//MainMenuViewController.m
Code:
#import "MainMenuViewController.h"
#import "SwitchViewController.h"
@implementation MainMenuViewController
-(IBAction)goToGlass
{
**//This is where I want to call the goToGlassRepairs method that is inside SwitchViewController's class...**
}
SwitchViewController是处理要显示和删除的笔尖的类。 MainMenu和glassRepairs是内容视图。但是,MainMenu需要访问switchviewcontroller类中的方法以允许从内容视图中导航。
我试过
`SwitchViewController *s = (SwitchViewController *)[[UIApplication sharedApplication] delegate];
[s goToGlassRepairs];`
它编译时没有警告,但会出现以下错误:
2010-03-09 15:51:28.350 Alfa2Go [29535:207] *** - [GoAppDelegate goToGlassRepairs]:无法识别的选择器发送到实例0x3b26620
2010-03-09 15:51:28.362 Go [29535:207] *由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'*
- [GoAppDelegate goToGlassRepairs]:无法识别的选择器发送到实例0x3b26620'
答案 0 :(得分:0)
好吧,我的猜测是[[UIApplication sharedApplication] delegate]
没有返回SwitchViewController
,而是返回GoAppDelegate
。您需要找到SwitchViewController
才能调用方法。
答案 1 :(得分:0)
您可以在MainMenu类中创建属性(例如id switchController)。 由于交换机视图控制器处理MainMenu的分配,它也可以将此属性设置为自己,因此MainMenu类将有一种方法来访问控制器......
答案 2 :(得分:0)
终于让这个工作了:
我所做的是在app delegate中添加了一个函数,该函数在switchview控制器中调用了display function。然后我将app委托导入我的主菜单,并创建了一个调用app delegate函数的函数。