2013-08-29 36 views
0

我是可可中的新手。現在我想在iOS中創建一個可選的消息框,但沒有任何按鈕。 Messagebox在NSTimer後自動關閉。我使用下面的代碼,但它總是添加OK按鈕。請給我任何建議。提前致謝。可以在可可中創建無按鈕的NSAlert

alert = [[[NSAlert alloc] init] autorelease]; 
    // [alert addButtonWithTitle: @"OK"]; 
    [alert setMessageText: @"Attention!!! This a critical Alert."]; 
    [alert setInformativeText:@"Scanning..."]; 
    [alert setAlertStyle: NSInformationalAlertStyle]; 

    NSTimer *myTimer = [NSTimer timerWithTimeInterval: 17.0 
               target:self 
              selector: @selector(killWindow:) userInfo:nil 
                   repeats:NO]; 

    [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSModalPanelRunLoopMode]; 

    int choice = 0; 
    choice = [alert runModal]; 
    if(choice != 0) 
     [myTimer invalidate]; 

KillWindow功能:

-(void) killWindow: (NSTimer *) theTimer 
{ 
    NSLog(@"killWindow"); 
    [[alert window] close]; 
} 

當警報關閉,我的應用程序不能點擊任何按鈕或互動?

回答

0

NSAlert旨在用於應用程序或窗口模式顯示的消息與用戶解除按鈕。它不是設計用於顯示沒有按鈕的窗口;你不應該這樣使用它。

您應該使用自定義的NSWindow/NSPanel。如果你想阻止窗口/應用程序,那麼你需要運行你自己的模態會話。除了像上面那樣關閉窗口之外,還可以使用abortModal從定時器回調中停止模式會話。這就解釋了爲什麼當警報關閉時你沒有得到任何進一步的事件 - 模態會話仍在運行。

查看How Modal Windows Work瞭解更多信息。

+0

謝謝,但你能爲我示例代碼? –

+0

[[的NSApplication sharedApplication] beginSheet:scanningPanel modalForWindow:窗口 modalDelegate:自 didEndSelector:@selector(sheetDidEnd:RETURNCODE:contextInfo :) contextInfo:無]; [[NSApplication sharedApplication] runModalForWindow:scanningPanel]; –

+0

我創建了自定義NSpanel,但我不知道如何爲它設置NSTimer。你可以幫我嗎?非常感謝 –

1

你應該看看MBProgressHUD。我使用MBProgressHUD來解決這個問題。

+ (void)showMessage:(NSString *)message forDuration:(NSTimeInterval)duration withTitle:(NSString *)title 
{ 
    UIWindow *window  = [UIApplication sharedApplication].keyWindow; 
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES]; 
    hud.mode    = MBProgressHUDModeText; 
    hud.labelText  = title; 
    hud.detailsLabelText = message; 

    hud.removeFromSuperViewOnHide = YES; 
    [hud hide:YES afterDelay:duration]; 
} 

經過一些用戶測試後,決定用戶不喜歡長時間的消息,他們不能解僱。

+ (void)showMessage:(NSString *)message forDuration:(NSTimeInterval)duration withTitle:(NSString *)title 
{ 
    if (duration < 3.0) { 
     UIWindow *window  = [UIApplication sharedApplication].keyWindow; 
     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES]; 
     hud.mode    = MBProgressHUDModeText; 
     hud.labelText  = title; 
     hud.detailsLabelText = message; 

     hud.removeFromSuperViewOnHide = YES; 
     [hud hide:YES afterDelay:duration]; 
    } else { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title 
                  message:message 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 
     [alertView show]; 

     int64_t delayInSeconds = duration; 
     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
      [alertView dismissWithClickedButtonIndex:alertView.cancelButtonIndex animated:YES]; 
     }); 
    } 
} 
+0

不,我執行MAC應用程序不是iOS應用程序。 –

+0

@JosonDaniel對不起,我的壞。不過,底層代碼是您可以從中解決問題的模型。 –

0

你關閉你不得不中止模式以及與[NSApp表示abortModal]警報窗口後

現在它只是一個隱藏了按鈕的問題。爲此,在實例化警報後,您可以在所有alert.window.contentView.subviews中掃描標題爲「OK」的NSButton並將其隱藏。

聲明:這是一個快速,醜陋的黑客攻擊,但它的工作原理...現在。使用需自擔風險

5

也許在2013年的答案是否定的。但是,在當前的SDK(Xcode 7.0與OS X 10.11)中,只需通過addButtonWithTitle(「」)添加「」,那麼該按鈕將不會顯示。

0

是的,你可以,你添加新的按鈕。之後,你明白它並隱藏它:)。我的代碼

NSAlert* alert = [[NSAlert alloc] init]; 
[alert setMessageText:@"Loading...."]; 
[alert addButtonWithTitle:@"Cancel"]; 
NSButton *button = [[alert buttons] objectAtIndex:0]; 
[button setHidden:YES]; 
[alert setAccessoryView:[self viewLoadingReadFile]]; 
[alert setAlertStyle:NSWarningAlertStyle]; 
[alert runModal]; 
相關問題