
这就是我的工作.
我有2个视图控制器和MainMenu.xib的窗口.
我还有一个AppController,它是两个视图控制器的委托.
当我启动应用程序时,我首先会遇到MainMenu.xib的Window视图,其中包含一个按钮.单击此按钮,IBAction将发送到appController,并要求SecondVIEwController显示它的nib.到目前为止,一切都很好,nib文件正确显示.
在secondVIEwController上,还有另一个按钮,它将另一个IBAction发送到appController,并要求显示FirstVIEwController,但没有任何反应,
没有崩溃,没有警告……任何帮助将不胜感激……
提前感谢您的耐心等待……
这是AppController.h的代码:
#import <Foundation/Foundation.h>#import "SecondVIEwController.h"#import "FirstVIEwController.h"@interface AppController : NSObject@property (strong) IBOutlet NSWindow *mainWindow;@property (strong) IBOutlet SecondVIEwController *secondVIEwController;@property (strong) IBOutlet FirstVIEwController *firstVIEwController;- (IBAction)secondbuttonfromsecondVIEwControllerClicked:(ID)sender;- (IBAction)buttonClicked:(ID)sender;@end
这是AppController.m的代码:
#import "AppController.h"@implementation AppController@synthesize mainWindow = mainwindow;@synthesize secondVIEwController;@synthesize firstVIEwController;- (IBAction)buttonClicked:(ID)sender { NSLog(@"button from second VIEw Controller clicked"); self.secondVIEwController = [[SecondVIEwController alloc]initWithNibname:@"SecondVIEwController" bundle:nil]; self.mainWindow.contentVIEw = self.secondVIEwController.vIEw; [self.secondVIEwController.vIEw setautoresizingMask:NSVIEwWIDthSizable | NSVIEwHeightSizable];} - (IBAction)secondbuttonfromsecondVIEwControllerClicked:(ID)sender { NSLog(@"button from first VIEwController clicked"); self.firstVIEwController = [[FirstVIEwController alloc]initWithNibname:@"FirstVIEwController" bundle:nil]; self.mainWindow.contentVIEw = [self.firstVIEwController vIEw];}@end 好吧,任何人都可以帮助我,我只需要一个视图应用程序,在第一个vIEwController上显示第一个带有按钮的VIEwController,它带我到第二个视图控制器,第二个按钮带我回到我的第一个vIEwcontroller ……我已经花了一个多星期的时间…徒劳…… PS:我不想在mainMenu.xib窗口上有任何按钮,也不需要标签.
解决方法 这是我的问题的解决方案.这是AppDelegate.h的代码:
// AppDelegate.h #import <Cocoa/Cocoa.h> #import "FirstVIEwController.h" #import "SecondVIEwController.h" //We need to declare the AppDelegate class as being the delegate for both //FirstVIEwController and SecondVIEwController @interface AppDelegate : NSObject <NSApplicationDelegate,FirstVIEwControllerDelegate,SecondVIEwControllerDelegate> @property (strong,nonatomic) NSWindow *window; @property (strong) FirstVIEwController *firstVIEwController; @property (strong) SecondVIEwController *secondVIEwController; -(voID) goToSecondVIEw; -(voID) goToFirstVIEw; @end
现在,这是AppDelegate.m:
// AppDelegate.m #import "AppDelegate.h" @implementation AppDelegate @synthesize window = _window; @synthesize firstVIEwController; @synthesize secondVIEwController; -(voID) awakeFromNib { [self goToFirstVIEw]; self.firstVIEwController.delegate = self; } -(voID) goToSecondVIEw { if (self.secondVIEwController ==nil) { self.secondVIEwController =[[SecondVIEwController alloc] initWithNibname:@"SecondVIEwController" bundle:nil]; } self.window.contentVIEw = [self.secondVIEwController vIEw]; } -(voID) goToFirstVIEw { if (self.firstVIEwController ==nil) { self.firstVIEwController =[[FirstVIEwController alloc] initWithNibname:@"FirstVIEwController" bundle:nil]; } self.window.contentVIEw = [self.firstVIEwController vIEw]; } @end 接下来,我们需要在FirstVIEwController和SecondVIEwController中设置委托
// FirstVIEwController.h#import <Cocoa/Cocoa.h>#import "SecondVIEwController.h"//We declare the delegation protocole:@protocol FirstVIEwControllerDelegate <NSObject>-(voID)goToSecondVIEw;@end@interface FirstVIEwController : NSVIEwController- (IBAction)firstVIEwControllerbuttonClicked:(ID)sender;@property (nonatomic,strong) ID <FirstVIEwControllerDelegate> delegate;@end
这是FirstVIEwController.m:
// FirstVIEwController.m #import "FirstVIEwController.h" @implementation FirstVIEwController @synthesize delegate; - (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]; if (self) { self.delegate = [NSApp delegate]; } return self; } - (IBAction)firstVIEwControllerbuttonClicked:(ID)sender { NSLog(@"button from first VIEw Controller clicked"); if ([self.delegate respondsToSelector:@selector(goToSecondVIEw)]) { [self.delegate goToSecondVIEw]; } } @end 现在,SecondVIEwController也是如此:
// SecondVIEwController.h #import <Cocoa/Cocoa.h> @protocol SecondVIEwControllerDelegate <NSObject> -(voID)goToFirstVIEw; @end @interface SecondVIEwController : NSVIEwController @property (nonatomic,strong) ID <SecondVIEwControllerDelegate> delegate; - (IBAction)goToFirstVIEwControllerbuttonClicked:(ID)sender; @end
这是SecondVIEwController.m:
// SecondVIEwController.m #import "SecondVIEwController.h" @interface SecondVIEwController () @end @implementation SecondVIEwController - (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]; if (self) { self.delegate = [NSApp delegate]; } return self; } - (IBAction)goToFirstVIEwControllerbuttonClicked:(ID)sender { NSLog(@"button from Second VIEw Controller clicked"); if ([self.delegate respondsToSelector:@selector(goToFirstVIEw)]) { [self.delegate goToFirstVIEw]; } } @end 好吧,我想这段代码可能会有所改进,如果您有任何建议,请随时告诉我.希望它会帮助别人.
总结以上是内存溢出为你收集整理的Mac OS X Cocoa多视图应用程序导航全部内容,希望文章能够帮你解决Mac OS X Cocoa多视图应用程序导航所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)