我正在研究我的第一个可可/ Objective-C应用程序,所以如果我做的事情明显不正确,请耐心等待.我将应用程序设置为将窗口中NSTextField中的任何内容复制到另一个NSTextField(在本例中为标签).如果用户未在文本框中输入任何内容,则应显示警报,但不是.我的代码出了什么问题?
AppDelegate.m:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize textBox1 = _textBox1;
@synthesize label1 = _label1;
- (void)dealloc
{
[super dealloc];
}
-(IBAction)setLabelTxt: (id)sender{
if(_textBox1.stringValue != @"")
[_label1 setStringValue: _textBox1.stringValue];
else{
NSAlert* msgBox = [[[NSAlert alloc] init] autorelease];
[msgBox setMessageText: @"You must have text in the text box."];
[msgBox addButtonWithTitle: @"OK"];
[msgBox runModal];
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
Run Code Online (Sandbox Code Playgroud)
此外,是否有任何Cocoa UI元素使用的方法指南(如命名方案)?我使用.NET风格的GUI编程.@结束
我正在尝试写这个警报:
func alertUser() {
let alert = NSAlert()
alert.messageText = "message 1"
alert.informativeText = "info1"
alert.informativeText = "info2"
alert.addButton(withTitle: "NO")
alert.addButton(withTitle: "YES")
alert.beginSheetModal(for: self.view.window!) { (returnCode: NSModalResponse) -> Void in
print ("returnCode: ", returnCode)
}
Run Code Online (Sandbox Code Playgroud)
但我收到了可怕的unexpectedly found nil while unwrapping an Optional value消息alert.beginSheetModal
请告诉我我做错了什么.
谢谢
编辑:我的问题不是很清楚,现在我对其进行了编辑,以明确我需要打开在线网页而不是帮助手册。
\n\n我想在 macOS 项目的 NSAlert 中包含一个问号按钮,该按钮指向带有帮助资源的在线网页。
\n\n我在这里看到有两种可能性:
\n\n\n\n\nvar showHelp: Bool 指定警报是否有帮助按钮。
\n\nvar helpAnchor:字符串?Alert\xe2\x80\x99s HTML 帮助锚点。
\n
但我不知道如何实现它。
\n\n我使用这段代码:
\n\n@IBAction func buttonPressed(_ sender: Any) {\n let myAlert: NSAlert = NSAlert()\n\n myAlert.messageText = "Message"\n myAlert.informativeText = "Informative text."\n\n myAlert.showsSuppressionButton = true\n\n myAlert.addButton(withTitle: "Later")\n myAlert.addButton(withTitle: "Now")\n myAlert.addButton(withTitle: "OK")\n\n let choice = myAlert.runModal()\n\n switch choice {\n case NSAlertFirstButtonReturn:\n print ("OK")\n case NSAlertSecondButtonReturn:\n print ("Now")\n case NSAlertThirdButtonReturn:\n print ("Later")\n default: break\n\n }\n if myAlert.suppressionButton!.state == …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我准备发布的 macOS 应用程序制作一本 Apple 帮助手册。然而,我正在尝试让锚点在我的 HTML 中工作。根据苹果的定义:
“锚点允许您唯一地标识帮助书中的主题。当用户点击指向锚点的链接时,帮助查看器会加载包含该锚点的页面。...您还可以使用锚点从应用程序中加载锚定页面,方法是:调用 NHelpManager 方法 openHelpAnchor:inBook: ...”
苹果的例子:<a name="ArrivalTimesUsingStopID"></a>
在我的 Apple 中,我有一个 NSAlert,它具有以下代码来显示帮助按钮,以便当您单击它时,它会打开指定的锚字符串。
alert.showsHelp = true
alert.helpAnchor = NSHelpManager.AnchorName(stringLiteral: "ArrivalTimesUsingStopID")
Run Code Online (Sandbox Code Playgroud)
运行代码确实会显示帮助按钮,Mac 帮助也会打开,但出现错误,指出找不到指定的内容。不知道为什么锚点不起作用,因为如果我转到帮助菜单并从那里打开它,我可以访问帮助手册。
此外,苹果公司的文件指出:
NSAlert、SFChooseIdentityPanel、SFCertificatePanel 类提供对话框的帮助按钮。要显示此类帮助按钮并将其链接到帮助书中的锚点,请在这些类中使用 setShowsHelp: 和 setHelpAnchor: 方法。
以及这些属性的文档NSAlert:
-setShowsHelp:YES 将帮助按钮添加到警报面板。当按下帮助按钮时,首先会咨询代表。如果委托未实现alertShowHelp:或返回NO,则使用nil book和-setHelpAnchor:指定的锚(如果有)调用-[NSHelpManager openHelpAnchor:inBook:]。如果委托返回 NO 并且没有设置帮助锚点,则会引发异常。
...所以我知道我正确使用了这两个。
我还了解到,每次更新 Apple 帮助手册 HTML 文档时都需要创建一个 .helpindex 文件。我正在使用“Help Indexer.app”,它位于developer.apple.com 上的其他Xcode 工具中。我确保:
<meta name="ROBOTS" content="ANCHORS">标头中,因此锚点会被索引。但即使完成了所有这些,我仍无法将 Apple 帮助手册打开到正确的锚点,甚至无法打开 Apple 帮助手册的标题页。
从头到尾多次,我无法找到解决方案或在线任何地方。 …
我已经使用了这个简单的通用方法来实现它,它适用于基于应用程序的对话框,但是我希望在工作表样式对话框中使用相同的功能,并且我很难将它们放在一起.
根据我理解的文档,OS10.9及更高版本中唯一不被弃用的方法是将NSAlert类与完成处理程序进程一起使用.这似乎使得从通用方法返回Bool几乎是不可能的.
我的代码:
-(BOOL)confirm :(NSString*)questionTitle withMoreInfo:(NSString*)addInfo andTheActionButtonTitle:(NSString*)actionType{
BOOL confirmFlag = NO;
NSAlert *alert = [NSAlert alertWithMessageText: questionTitle
defaultButton:actionType
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"%@",addInfo];
[alert setAlertStyle:1];
NSInteger button = [alert runModal];
if(button == NSAlertDefaultReturn){
confirmFlag = YES;
}else{
confirmFlag = NO;
}
return confirmFlag;
}
The [alert runModal] returns the value I can return.
Run Code Online (Sandbox Code Playgroud)
使用较新的范例,[alert beginSheetModalForWindow:[self window] sheetWindow completionHandler:some_handler]不允许我更新或返回方法结尾的值.我知道为什么,但有没有办法我不想做到这一点.
请告诉我如何创建一个类似于我用于工作表的方法.
谢谢三重
当 Big Sur 进入我们的 Mac 时,我注意到NSAlert控制器中破坏性按钮的文本呈红色,如图所示。
我无法找到一种方法在我的应用程序中引入此功能。
使用标准addButton(withTitle:)方法,我们没有任何方法来设置其意图(例如默认、取消或破坏性)。
你能给我任何提示吗?
谢谢
我有几个带有不同文本的NSAlert对话框.我想将警报窗口宽度调整为文本,以便文本不会换行.因此我使用此代码来计算字符串的宽度:
NSSize size = [myString sizeWithAttributes:@{NSFontAttributeName: [NSFont systemFontOfSize:[NSFont systemFontSize]]}];
Run Code Online (Sandbox Code Playgroud)
然后我尝试调整警报的窗口:
NSAlert *alert = [NSAlert alertWithMessageText:...
...
NSPanel *panel = alert.window;
NSRect frame = panel.frame;
float x = ((NSTextField*)[[((NSPanel*)(alert.window)).contentView subviews] objectAtIndex:5]).frame.origin.x; //the x-position of the NSTextField
frame.size.width = size.width + x;
[alert.window setFrame:frame display:YES];
Run Code Online (Sandbox Code Playgroud)
这段代码有效,但我第一次用这段代码调用方法.如果我再取一个字符串并再次调用该方法,则窗口不会调整大小(尽管计算出的宽度不同).
任何想法,我如何调整NSAlert窗口的大小?
我有一个带有两个按钮的NSAlert:
var al = NSAlert()
al.informativeText = "You earned \(finalScore) points"
al.messageText = "Game over"
al.showsHelp = false
al.addButtonWithTitle("New Game")
al.runModal()
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我不知道如何识别,用户按下了哪个按钮.
我正在使用 anNSAlert在我的应用程序的主屏幕上显示错误消息。基本上,这NSAlert是我的主视图控制器的一个属性
class ViewController: NSViewController {
var alert: NSAlert?
...
}
Run Code Online (Sandbox Code Playgroud)
当我收到一些通知时,我会显示一些消息
func operationDidFail(notification: NSNotification)
{
dispatch_async(dispatch_get_main_queue(), {
self.alert = NSAlert()
self.alert.messageText = "Operation failed"
alert.runModal();
})
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我收到多个通知,每个通知都会显示警报。我的意思是,它与第一条消息一起显示,我单击“确定”,它消失,然后再次与第二条消息一起显示等等......这是正常行为。
我想要实现的是避免这一系列错误消息。其实我只关心第一个。有没有办法知道我的警报视图当前是否正在显示?就像alert.isVisible在 iOS 上一样UIAlertView?
我正在MacOS上的Swift 4中使用NSAlert将登录对话框放在一起。这是我到目前为止的代码:
func dialogOKCancel(question: String, text: String) -> (String, String) {
let alert = NSAlert()
alert.messageText = question
alert.informativeText = text
alert.alertStyle = .warning
alert.addButton(withTitle: "Login")
alert.addButton(withTitle: "Cancel")
let unameField = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
let passField = NSSecureTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
let stackViewer = NSStackView()
stackViewer.addSubview(unameField)
alert.accessoryView = stackViewer
let response: NSApplication.ModalResponse = alert.runModal()
if (response == NSApplication.ModalResponse.alertFirstButtonReturn) {
return (unameField.stringValue, passField.stringValue)
} else {
return ("", "")
} …Run Code Online (Sandbox Code Playgroud) nsalert ×10
cocoa ×5
swift ×5
macos ×4
objective-c ×3
appkit ×1
apple-help ×1
button ×1
macos-sierra ×1
nsview ×1
xcode ×1