这就是我的问题的解决方案。
这是 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
好吧,我想这段代码可能会得到改进,如果您有任何建议,请随时告诉我。希望它能帮助其他人。