我使用XCode的Master/Detail模板编写了Master/Detail应用程序.启动应用程序后,主视图的导航按钮标题只是"主".现在我想重命名那个按钮,但不幸的是我不知道如何访问这个按钮.在appdelegate.m中,有以下代码来初始化视图:
MasterViewController *masterViewController = [[MasterViewController alloc] init];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController]
Run Code Online (Sandbox Code Playgroud)
我尝试了这些方法没有成功:
masterViewController.navigationItem.title = @"newTitle";
Run Code Online (Sandbox Code Playgroud)
要么
masterNavigationController.navigationItem.title = @"newTitle";
Run Code Online (Sandbox Code Playgroud)
由于我不确定,如果按钮的名称只是idt后面的视图的标题,我也尝试过:
masterViewController.title = @"newTitle";
没有任何效果.但由于按钮的标题是"Master",我绝对没有设置它,我相信必须有一些方法来设置它.有谁知道怎么做?
只是为了显示按钮:
我很久以后就开始了一个新的Cocoa项目......我不知道为什么,但是当我通过NSWindowController调用xib时总是会出错.我所做的非常简单:我有一个新项目作为起点,然后我不想从Appdelegate调用xib,而是从NSWindowController的子类调用.然后输出告诉我:
2014-11-12 09:58:18.519 SimpleTest [8554:378690] ApplePersistence = NO
2014-11-12 09:58:18.671 SimpleTest [8554:378690]无法连接(窗口)出口(NSApplication)到(NSWindow):缺少setter或实例变量
好的,它在代码中看起来如何?我的Appdelegate看起来像这样:
#import "AppDelegate.h"
#import "MainWindowController.h"
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@property (strong) MainWindowController *mainWindowController;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
_mainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainMenu"];
[self.mainWindowController showWindow:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
到目前为止没什么特别的.该MainWindowController如下所示:
#import "MainWindowController.h"
@interface MainWindowController ()
@property (weak) IBOutlet NSWindow *window;
@end
@implementation MainWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self != nil)
{
//do something
}
return self;
} …Run Code Online (Sandbox Code Playgroud) 实际上我收到了一个奇怪的例外:我迭代一个MutableDictionary并想在其中设置一个新值:
selectionIndex = [NSMutableDictionary dictionaryWithDictionary:selection];
NSString *whatever = @"999999";
id keys;
NSEnumerator *keyEnum = [selectionIndex keyEnumerator];
while (keys = [keyEnum nextObject])
{
[selectionIndex setObject:whatever forKey:keys];
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,传递给这个方法的选择是一个MutableDictionary.如果我运行此代码,我收到以下异常:
2011-12-05 15:28:05.993 lovelini [1333:207] *由于未捕获的异常'NSGenericException'终止应用程序,原因:'* Collection <__ NSCFDictionary:0x6a33ed0>在枚举时发生了变异.{type = mutable dict,count = 8,entries => 0:{contents =
好吧,我知道我不能改变NSDictionary,但据我所知,我不知道!那么为什么我会得到这个例外呢?这是快速枚举的限制吗?据我所知,无法在Fast Enumeration中添加或删除条目,但我不添加或删除任何内容?!