Mac OS X Cocoa多视图应用程序导航 [英] Mac OS X Cocoa multiview application navigation
问题描述
我已花了2整天试图找出如何使用NSViewControllers来创建多视图应用程序。
这是我做的。 p>
我有2个视图控制器和MainMenu.xib的窗口。
我也有一个AppController是两个视图控制器的委托。
当我启动应用程序,我首先迎接MainMenu.xib的窗口保存按钮的视图。当点击这个按钮,IBAction被发送到appController,并要求SecondViewController显示它的nib。到目前为止,一切正常,并且nib文件显示正确。
在secondViewController上,有另一个按钮发送另一个IBAction到appController,并要求显示FirstViewController但没有发生,
没有崩溃,没有警告...任何帮助将非常感激...
提前感谢您的耐心...
这里是AppController.h的代码:
#import< Foundation / Foundation.h>
#importSecondViewController.h
#importFirstViewController.h
@interface AppController:NSObject
@property * mainWindow;
@property(strong)IBOutlet SecondViewController * secondViewController;
@property(strong)IBOutlet FirstViewController * firstViewController;
- (IBAction)secondButtonfromsecondViewControllerClicked:(id)sender;
- (IBAction)buttonClicked:(id)sender;
@end
这里是AppController.m的代码:
#importAppController.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:@SecondViewControllerbundle:nil];
self.mainWindow.contentView = self.secondViewController.view;
[self.secondViewController.view setAutoresizingMask:NSViewWidthSizable |
NSViewHeightSizable];
}
- (IBAction)secondButtonfromsecondViewControllerClicked:(id)sender {
NSLog(@按钮从第一个ViewController点击);
self.firstViewController = [[FirstViewController
alloc] initWithNibName:@FirstViewControllerbundle:nil];
self.mainWindow.contentView = [self.firstViewController view];
}
@end
好吧,任何人都可以帮助我,我只需要一个视图应用程序显示第一个ViewController与第一个viewController上的按钮,我带到第二个视图控制器,第二个按钮,我带回到我的第一个viewcontroller。 。我已经花了一个多星期的时间... in vain ... PS:我不想在mainMenu.xib窗口或标签上的任何按钮。
这里是AppDelegate.h的代码:
这里是我的问题的解决方案。 // AppDelegate.h
#import< Cocoa / Cocoa.h>
#importFirstViewController.h
#importSecondViewController.h
//我们需要将AppDelegate类声明为
/ / FirstViewController和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
#importAppDelegate.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:@SecondViewControllerbundle:nil];
}
self.window.contentView = [self.secondViewController view];
}
- (void)goToFirstView {
if(self.firstViewController == nil){
self.firstViewController = [[FirstViewController alloc]
initWithNibName:@FirstViewControllerbundle:nil];
}
self.window.contentView = [self.firstViewController view];
}
@end
需要在FirstViewController和SecondViewController中设置委托
// FirstViewController.h
#import< ; Cocoa / Cocoa.h>
#importSecondViewController.h
//我们声明委托协议:
@protocol FirstViewControllerDelegate< NSObject>
- (void)goToSecondView;
@end
@interface FirstViewController:NSViewController
- (IBAction)firstViewControllerButtonClicked:(id)sender;
@property(nonatomic,strong)id< FirstViewControllerDelegate>代表;
@end
这里是FirstViewController.m:
// FirstViewController.m
#importFirstViewController.h
@实现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 responsesToSelector:@selector(goToSecondView)]){
[self.delegate goToSecondView];
}
}
@end
,SecondViewController的同样的东西:
// SecondViewController.h
#import< Cocoa /可可。
@protocol SecondViewControllerDelegate< NSObject>
- (void)goToFirstView;
@end
@interface SecondViewController:NSViewController
@property(nonatomic,strong)id< SecondViewControllerDelegate>代表;
- (IBAction)goToFirstViewControllerButtonClicked:(id)sender;
@end
这里是SecondViewController.m:
// SecondViewController.m
#importSecondViewController.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(@按钮从第二个视图控制器单击);
if([self.delegate responsesToSelector:@selector(goToFirstView)]){
[self.delegate goToFirstView];
}
}
@end
,我想这个代码可能会改进,如果你有任何建议,随时让我知道。希望它会帮助别人。
I've already spent 2 full days trying to figure out how to use NSViewControllers in order to create a multiview application.
Here is what I do.
I have 2 View Controllers and the MainMenu.xib's Window. I also have an AppController that is the delegate for both View Controllers.
When I launch the app, I'm first greeted with the MainMenu.xib's Window's view which holds a button. On clicking this button, an IBAction is sent to the appController and asks for the SecondViewController to display it's nib. So far, everything's fine and the nib file is displayed correctly.
On the secondViewController, there's another button that sends another IBAction to the appController and asks for the FirstViewController to be displayed but nothing happens, no crash, no warning... Any help would be much appreciated... Thanks in advance for your patience...
Here is the code for the 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
and here is the code for the 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
Well, anyone can help me, I just need a single view application that displays a first ViewController with a button on the first viewController that takes me to a second view controller with a second button that takes me back to my first viewcontroller... I've already spent more than a week on that... in vain... PS : I don't want any button on the mainMenu.xib window nor tabs.
here is the solution to my question then.
Here's the code for the 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
Now, here's the 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
Next we need to set delegates in the FirstViewController and the 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
And here is the 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
Now, same thing for the 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
And here's the 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
Well, I guess this code may be improved and if you have any suggestion, feel free to let me know. Hope it will help others.
这篇关于Mac OS X Cocoa多视图应用程序导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!