2012-04-13 21 views
12

申し訳ありませんが、これは愚かな質問のように見えるが、私はインターネットを検索する最後の30分を過ごしたし、私の質問への答えを見つけることができません。私は本当にシンプルなものを逃してしまったので、これが期待されています。XCode Cocoa App Preferences?

私はMac用のXCodeで簡単なアプリケーションを作成しました。これはうまく構築されてコンパイルされます。

どのようにして設定用のメニューを作成しますか?簡単な方法はありますか、新しいインターフェースを作成する必要はありますか?どのようにしてこれらのプリファレンスに値を入れて入れますか?

チュートリアルは1つ見つかりましたが、iOS用で、Mac用に開発している場合は「設定バンドル」が表示されません。

ありがとうございます。

EDIT:具体的にhttps://developer.apple.com/cocoa/cocoabindings.html

+3

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/UserDefaults/StoringPreferenceDatainiCloud/StoringPreferenceDatainiCloud.html#/:

ここAppDelegate.mの主要部分です/ apple_ref/doc/uid/10000059i-CH7-SW7 – mydogisbox

+0

おかげさまで、読んでいただきありがとうございます – Cristian

+0

「EDIT」リンクは一般的なCocoaドキュメントになりました。私はこのリンクがあなたが参照しているものだと思っています:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html –

答えて

15

This is the documentation you want. How to provide a Preferences Interface:以下のリンクは、このために最適です。

他のウィンドウと同じように独自の設定メニュー/ウィンドウを作成し、ユーザの設定が保存されているユーザデフォルト辞書に保存されている値を変更できるようにする必要があります。通常は保存されています(ココアバインディングが非常に簡単に行うプロセス)。

それ以外の場合は、手動で[NSUserDefaults standardUserDefaults]という参照が必要なので、-setObject:ForKey:などとすることができます。上記にリンクされた文書全体(他のセクションを含む)を読むことが賢明でしょう。

NSUserDefaultsとインターフェイスして設定を保存/取得する方法については、Apple docs hereを参照してください。

+2

パネルを作成し、必要なバインディングを作成するために、次のリンクを推奨します:https://developer.apple.com/cocoa/cocoabindings.html – Cristian

2
+0

ドキュメントはicloudと環境設定を同期する方法を示しています。私が望むものではなく、好みを取得して設定する方法を示しています。しかし、どのように設定ウィンドウを作成するのか、それをどのようにリンクするのかは分かりません。 – Cristian

+2

私はちょうど同じ文書を、間違ったセクションをリンクしました。今より良いはずです。 – mydogisbox

+0

これらのドキュメントは多少スケッチです。あなたは他の場所を見る必要があります。 –

6

ウィンドウ自体については、RHPreferencesフレームワークをお勧めします。

入手可能GitHubで利用可能です。 BSDライセンス

あなたの次のMacアプリケーション用の複数のタブを備えたシンプルで簡単なPreferencesウィンドウコントローラです。

また提供:最後に使用したタブの異なるサイズのタブのビュー間をリサイズ

  • オート(動画付き)
  • カスタムNSToolbarItemサポート
  • 永続
  • プレースホルダNSToolbarItemsのサポート(例えばNSToolbarFlexibleSpaceItemIdentifier & NSToolbarShowFontsItemIdentifier)
+1

ARCサポートが必要な場合は、RHPreferences 'ネットワークグラフ。プロジェクトをARCに変換するこのリポジトリを見つけました。ウィンドウのサイズを変更するための完了ブロックも含まれています。 https://github.com/vilhalmer/RHPreferences –

0

プロジェクトのMainMenu.xibには、アプリ名をクリックすると、ドロップダウンリストが表示されます。 Cntrl +クリック+ PreferencesをプロジェクトのAppDelegate.hファイルにドラッグして作成し、IBActionメソッドを作成します。

xibでNSWindowController(PreferenceWindowController)でクラスを作成し、そのPreferenceWindowControllerの強力なプロパティを作成し、initを割り当て、[self.preferenceWindowController showWindow:self];をAppDelegate.mに追加します。これにより、お使いのOS Xアプリケーション用のウィンドウが作成されます(Preferences)。

0

各設定パネルの.nibとコントローラ(.h & .m)を作成します。 その後、AppのAppDelegate.mで動的にフックします。 私はアプリで、動的にロードされた番号のバンドルで構成されているバンドルを使用しています。つまり、各バンドルには独自の設定があります。あなたがここで本当に良い簡潔な例で見ることができます

:この例では http://www.knowstack.com/nstoolbar-sample-code-objectivec/

は、それが動的にNSToolbarとNSToolbarItemを作成します。 各設定パネルの各ウィンドウコントローラーで行うことは、あなた次第です。

// AppDelegate.m 
// CocoaToolBars 
// Created by Debasis Das on 4/30/15. 
// Copyright (c) 2015 Knowstack. All rights reserved. 

#import "AppDelegate.h" 
@interface AppDelegate() 
@property (weak) IBOutlet NSWindow *window; 
@end 

@implementation AppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    // Insert code here to initialize your application 
    _toolbarTabsArray = [self toolbarItems]; 
    _toolbarTabsIdentifierArray = [NSMutableArray new]; 

    for (NSDictionary *dict in _toolbarTabsArray){ 
    [_toolbarTabsIdentifierArray addObject:dict[@"identifier"]]; 
    } 
    _toolbar = [[NSToolbar alloc] initWithIdentifier:@"ScreenNameToolbarIdentifier"]; 
    _toolbar.allowsUserCustomization = YES; 
    _toolbar.delegate = self; 
    self.window.toolbar = _toolbar; 
} 

-(NSArray *)toolbarItems { 
    NSArray *toolbarItemsArray = [NSArray arrayWithObjects: 
           [NSDictionary dictionaryWithObjectsAndKeys:@"Find Departments",@"title",@"Department-50",@"icon",@"DepartmentViewController",@"class",@"DepartmentViewController",@"identifier", nil], 
           [NSDictionary dictionaryWithObjectsAndKeys:@"Find Accounts",@"title",@"Business-50",@"icon",@"AccountViewController",@"class",@"AccountViewController",@"identifier", nil], 
           [NSDictionary dictionaryWithObjectsAndKeys:@"Find Employees",@"title",@"Edit User-50",@"icon",@"EmployeeViewController",@"class",@"EmployeeViewController",@"identifier", nil], 
           nil]; 
    return toolbarItemsArray; 
} 

- (void)applicationWillTerminate:(NSNotification *)aNotification { 
    // Insert code here to tear down your application 
} 

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar 
    itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag { 
    NSDictionary *itemInfo = nil; 

    for (NSDictionary *dict in _toolbarTabsArray) { 
    if([dict[@"identifier"] isEqualToString:itemIdentifier]) { 
     itemInfo = dict; 
     break; 
    } 
    } 

    NSAssert(itemInfo, @"Could not find preferences item: %@", itemIdentifier); 

    NSImage *icon = [NSImage imageNamed:itemInfo[@"icon"]]; 
    if(!icon) { 
    icon = [NSImage imageNamed:NSImageNamePreferencesGeneral]; 
    } 
    NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier]; 
    item.label = itemInfo[@"title"]; 
    item.image = icon; 
    item.target = self; 
    item.action = @selector(viewSelected:); 
    return item; 
} 

-(void)viewSelected:(id)sender { 
    NSToolbarItem *item = sender; 
    [self loadViewWithIdentifier:item.itemIdentifier withAnimation:YES]; 
} 

-(void)loadViewWithIdentifier:(NSString *)viewTabIdentifier 
         withAnimation:(BOOL)shouldAnimate { 
    NSLog(@"viewTabIdentifier %@",viewTabIdentifier); 

    if ([_currentView isEqualToString:viewTabIdentifier]) { 
    return; 
    } else { 
    _currentView = viewTabIdentifier; 
    } 
    //Loop through the view array and find out the class to load 

    NSDictionary *viewInfoDict = nil; 
    for (NSDictionary *dict in _toolbarTabsArray) { 
    if ([dict[@"identifier"] isEqualToString:viewTabIdentifier]) { 
     viewInfoDict = dict; 
     break; 
    } 
    } 
    NSString *class = viewInfoDict[@"class"]; 
    if(NSClassFromString(class)) { 
    _currentViewController = [[NSClassFromString(class) alloc] init]; 
    NSView *newView = _currentViewController.view; 

    NSRect windowRect = self.window.frame; 
    NSRect currentViewRect = newView.frame; 

    windowRect.origin.y = windowRect.origin.y + (windowRect.size.height - currentViewRect.size.height); 
    windowRect.size.height = currentViewRect.size.height; 
    windowRect.size.width = currentViewRect.size.width; 

    self.window.title = viewInfoDict[@"title"]; 
    [self.window setContentView:newView]; 
    [self.window setFrame:windowRect display:YES animate:shouldAnimate];  
    } else { 
    NSAssert(false, @"Couldn't load %@", class); 
    } 
} 

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar { 
    NSLog(@"%s %@",__func__,_toolbarTabsIdentifierArray); 
    return _toolbarTabsIdentifierArray; 
} 

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar { 
    NSLog(@"%s",__func__); 
    return [self toolbarDefaultItemIdentifiers:toolbar]; 
} 

- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar { 
    NSLog(@"%s",__func__); 
return [self toolbarDefaultItemIdentifiers:toolbar]; 
} 

- (void)toolbarWillAddItem:(NSNotification *)notification { 
    NSLog(@"%s",__func__); 
} 

- (void)toolbarDidRemoveItem:(NSNotification *)notification { 
    NSLog(@"%s",__func__); 
} 

@end 
関連する問題