Mac OS X Cocoa 多视图应用程序导航

2023-12-12

我已经花了整整 2 天的时间试图弄清楚如何使用 NSViewControllers 来创建多视图应用程序。

这就是我所做的。

我有 2 个视图控制器和 MainMenu.xib 的窗口。 我还有一个 AppController,它是两个视图控制器的委托。

当我启动该应用程序时,首先映入眼帘的是 MainMenu.xib 的窗口视图,其中包含一个按钮。单击此按钮后,IBAction 会发送到 appController 并要求 SecondViewController 显示其笔尖。到目前为止,一切都很好,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 上有一个按钮,将我带到第二个视图控制器,第二个按钮将我带回我的第一个视图控制器......我已经花了一个多星期的时间了……徒劳无功…… 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

好吧,我想这段代码可能会得到改进,如果您有任何建议,请随时告诉我。希望它能帮助其他人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mac OS X Cocoa 多视图应用程序导航 的相关文章

  • 我应该为每个选项卡栏使用单独的 UINavigationController

    根据Apple https developer apple com library ios documentation WindowsViews Conceptual ViewControllerCatalog Chapters Combi
  • 基于视图的 NSTableView 在插入带有动画的新行后渲染空白行

    我有一个基于视图的NSTableView我用 NSMutableArray 来支持 我定期出去获取一些数据 并希望将新行插入到顶部的表中 当我在没有指定动画的情况下执行此操作时insertRowsAtIndexes withAnimatio
  • 检测可可上的鼠标右键单击

    我正在尝试在我的原型中管理鼠标事件精灵套件 game 我从问题中使用了以下方法SO q https stackoverflow com questions 17473176 how to detect right and left clic
  • 在 Cocoa 中声明对象时,我应该将它们设置为 nil 吗?

    假设我想创建一个实例NSString 根据另一个变量的值初始化为某个值 通常情况下 我会这样做 NSString string if foo 1 string Foo is one else string Foo is not one 然而
  • 似乎无法在 NSMenuItem 上 setEnabled:NO

    我已经子类化了NSMenu并连接了一堆NSMenuItem通过界面生成器 我已经通过调试器进行了测试 看看它们确实得到了初始化 菜单设置为不自动启用项目 仍然当我将任何 NSMenuItem 设置为 myMenuItem setEnable
  • 调度队列:如何判断它们是否正在运行以及如何停止它们

    我只是在玩 GCD 并且编写了一个 CoinFlipper 玩具应用程序 抛硬币的方法如下 void flipCoins NSUInteger nFlips Create the queues for work dispatch queue
  • 在 Objective C 中获取第一响应者

    我无法弄清楚哪个UITextField是当前的第一响应者 我想做的是 如果用户单击特定的内容 则设置一个布尔值UITextField 因此 要做到这一点 我需要能够判断这个特定的文本字段是否已成为第一响应者 我知道如何设置第一响应者 但只是
  • TextView、iOS 和 OSX 中的彩虹文本

    我正在尝试向我的应用程序添加一些不同的文本颜色以融合到图像中 我收到了很多用户想要彩虹文本颜色并重复的输入 例如这个词 stackoverflow看起来像这样 s red t orange a yellow c green k blue o
  • 使用常量 NSString 作为 NSUserDefaults 的键

    我正在使用 NSUSerDefaults 来存储用户首选项 我记得在某处读到过 将键设置为常量是一个好主意 我同意 以下代码是我目前拥有的 NSUserDefaults standardUserDefaults setObject NSNu
  • 如何使用QLPreviewPanel?

    我该如何使用QLPreviewPanel 我知道它以前不是公共 API 但在 10 6 中已经有了 如何使用它在标准 QuickLook 面板中显示文件预览 查看快速查看下载器 http developer apple com librar
  • 使用 pyobjc 将元数据写入 pdf

    我正在尝试使用以下 python 代码将元数据写入 pdf 文件 from Foundation import from Quartz import url NSURL fileURLWithPath test pdf pdfdoc PDF
  • 多视图几何

    我从相距一定距离的两台相同品牌的相机捕获了两张图像 捕获了相同的场景 我想计算两个相机之间的现实世界旋转和平移 为了实现这一点 我首先提取了两张图像的 SIFT 特征并进行匹配 我现在有基本矩阵也单应性矩阵 然而无法进一步进行 有很多混乱
  • NSPredicate 格式字符串不起作用

    在我的代码中 我想检查记录是否已存在 以便我知道是创建它还是更新它 但我遇到了一个问题 问题是当我使用这个时 NSPredicate pred NSPredicate predicateWithFormat ATTRIBUTE ID idN
  • WebView 不运行 loadHTMLString 中给出的 JavaScript

    我不明白为什么这不起作用 我的桌面上有一个 test htm 文件 如下所示 This is x 2 我有一个 WebVie
  • 每个 CPU 核心处于 C0 电源状态的时间

    任何帮助弄清楚如何做到这一点都会很棒 在过去一秒内 每个 CPU 核心处于 C0 电源状态的时间有多少 这是针对 Mac 应用程序的 因此需要 Objective C cocoa 和 c OS X 没有任何公开 CPU c 状态的 API
  • 为什么在 10.5 上我必须在 NSWindowController 上调用 showWindow 两次?

    我有一个 NSWindowController 的子类 我用它从笔尖加载窗口并将其显示在屏幕上 下面是当我想显示窗口时调用的代码 在 10 6 上 当调用 showCustomWindow 时 会显示窗口 但在 10 5 上 必须调用此方法
  • Apple 如何在机场菜单打开时更新它? (当 NSMenu 已经打开时如何更改它)

    我有一个状态栏项目 可以弹出一个 NSMenu 并且我有一个委托集 并且它已正确连接 void menuNeedsUpdate NSMenu menu工作正常 也就是说 该方法设置为在显示菜单之前调用 我需要监听该方法并触发异步请求 稍后在
  • 无法按住 CTRL+拖动 NSButton 到自定义 NSView 标题

    我想创建一个自定义的NSTableCellView由 Interface Builder 实例化 我已将 Table Cell View 类设置为MyTableCellView 并正确创建MyTableCellView NSTableCel
  • 使用 NSError 检查错误的正确结构

    我正在编写各种例程 并尽力保持其整洁和重构 我正在创建的方法开始看起来与此代码类似 IBAction buttonPress id sender Create Document Shopping List with this documen
  • 为沙盒 Cocoa 应用程序创建临时文件

    我的应用程序是沙箱化的 根据最新的应用程序商店指南 我想创建一些临时文件 我可以这样做吗 如果 是 我可以在哪里这样做 有没有预先指定的路径 还有访问该路径的命令 您应该使用NSTemporaryDirectory 函数 它将查找并返回适合

随机推荐