2011-04-22 2 views
3

일반 앱으로 실행되는 애플리케이션이 있지만 NSStausItem도 있습니다. 환경 설정에서 체크 박스를 설정하는 기능을 구현하고 싶었고이 체크 박스가 켜져 있으면 상태 항목이 표시되어야하지만 체크 박스가 꺼져 있으면 상태 항목이 제거되거나 보이지 않아야합니다. NSStatusItem을 표시하거나 숨기려면 환경 설정 저장

내가 여기 포럼에 비슷한 문제에 직면 사람을 발견 : How do you toggle the status item in the menubar on and off using a checkbox?

을하지만이 솔루션이 문제는 예상대로 작동하지 않습니다. 그래서이 확인란을 선택하면 모든 것이 잘 동작하지만, 응용 프로그램을 두 번 열면 응용 프로그램이 처음 실행했을 때 선택한 선택을 인식하지 못합니다. 확인란이 BOOL 또는 그 외의 것에 바인딩되어 있지 않기 때문에 체크 박스에는 IBAction 만 있으며 런타임에 상태 항목을 제거하거나 추가합니다.

내 질문은 : 어떻게 상황 항목을 표시할지 여부를 선택할 수있는 환경 설정에서 확인란을 만들 수 있습니다. 좋아


실제로 내가 시도 내가 복사 한 다음 내가 당신에게 AppDelegate.h에서 링크

준 포스트에서 다음 Delegate.m에

NSStatusItem *item; 
NSMenu *menu; 
IBOutlet NSButton myStatusItemCheckbox; 

다음 :

- (BOOL)createStatusItem 
{ 
NSStatusBar *bar = [NSStatusBar systemStatusBar]; 

//Replace NSVariableStatusItemLength with NSSquareStatusItemLength if you 
//want the item to be square 
item = [bar statusItemWithLength:NSVariableStatusItemLength]; 

if(!item) 
    return NO; 

//As noted in the docs, the item must be retained as the receiver does not 
//retain the item, so otherwise will be deallocated 
[item retain]; 

//Set the properties of the item 
[item setTitle:@"MenuItem"]; 
[item setHighlightMode:YES]; 

//If you want a menu to be shown when the user clicks on the item 
[item setMenu:menu]; //Assuming 'menu' is a pointer to an NSMenu instance 

return YES; 
} 


- (void)removeStatusItem 
{ 
NSStatusBar *bar = [NSStatusBar systemStatusBar]; 
[bar removeStatusItem:item]; 
[item release]; 
} 


- (IBAction)toggleStatusItem:(id)sender 
{ 
BOOL checked = [sender state]; 

if(checked) { 
    BOOL createItem = [self createStatusItem]; 
    if(!createItem) { 
    //Throw an error 
    [sender setState:NO]; 
    } 
} 
else 
    [self removeStatusItem]; 
} 

다음에 IBaction에서 나는 이것을 더한다 :

,
[[NSUserDefaults standardUserDefaults] setInteger:[sender state] 
               forKey:@"MyApp_ShouldShowStatusItem"]; 

내로 awakeFromNib에 내가이 일 추가 :`

NSInteger statusItemState = [[NSUserDefaults standardUserDefaults] integerForKey:@"MyApp_ShouldShowStatusItem"]; 
[myStatusItemCheckbox setState:statusItemState]; 

그런 다음 인터페이스 빌더에서 나는 새로운 확인란을 "myStatusItemCheckbox"로 연결 생성과 IBAction를 추가도 내가 바인딩 관리자를 클릭하고 다음 바인딩에 값을 설정하십시오. NSUserDefaultControllerModelKeyPath으로 설정합니다. MyApp_ShouldShowStatusItem. 불행히도이 작업이 전혀 잘못되었습니다.

답변

7

당신이해야 할 일은 User Defaults 시스템을 사용하는 것입니다. 환경 설정을 저장하고로드하는 것을 매우 쉽게 만듭니다. 버튼의 동작에서

, 당신은 그 상태를 저장합니다 :
- (IBAction)toggleStatusItem:(id)sender { 

    // Your existing code... 

    // A button's state is actually an NSInteger, not a BOOL, but 
    // you can save it that way if you prefer 
    [[NSUserDefaults standardUserDefaults] setInteger:[sender state] 
               forKey:@"MyApp_ShouldShowStatusItem"]; 
} 

와의

이 앱 대리인의 (또는 다른 적절한 객체) awakeFromNib, 당신은 사용자의 기본 설정에서 그 값을 다시 읽습니다 :

NSInteger statusItemState = [[NSUserDefaults standardUserDefaults] integerForKey:@"MyApp_ShouldShowStatusItem"]; 
[myStatusItemCheckbox setState:statusItemState]; 

그리고 필요한 경우 removeStatusItem으로 전화하십시오.

이 절차는 저장하려는 거의 모든 환경 설정에 적용됩니다.

+0

고맙습니다. 나중에 시도해 보겠습니다.) – dehlen

+2

값을 사용해야합니다. 값을 유지하고 체크 박스에서 값을 검색하는 대신 컨트롤러는 상태 항목이 상태 표시 줄에 직접 있어야하는지 여부에 대한 지식을 소유해야합니다.이것은 바이너리 선택이므로 부울 값이어야하며 컨트롤러는 기본값으로 그 값을 저장하고 부울 값을 기준으로 NSOnState 또는 NSOffState로 체크 박스를 설정해야합니다. ('NSOnState'와'YES'는 동일한 수로 정의되고, NSOffState와'NO'도 마찬가지입니다. 그러나 명확성과 설명은 미덕입니다.) –