如何创建和获取返回值从Cocoa对话框? [英] How to create and get return Value from Cocoa Dialog?
本文介绍了如何创建和获取返回值从Cocoa对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我想创建一个对话框,其中有一个文本字段和一个按钮
,通过它我可以提示用户并获取用户输入的值。
如何在可可,目标C中做到这一点?
我没有找到任何预定义的方法。
<你可以调用NSAlert并将NSTextField作为它的attachmentView,像这样
code> - (NSString *)input:(NSString *)prompt defaultValue:(NSString *)defaultValue {
NSAlert * alert = [NSAlert alertWithMessageText:prompt
defaultButton:@OK
alternateButton:@Cancel
otherButton:nil
informativeTextWithFormat:@];
NSTextField * input = [[NSTextField alloc] initWithFrame:NSMakeRect ,200,24)];
[input setStringValue:defaultValue];
[input autorelease];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if(Button == NSAlertDefaultReturn){
[input validateEditing];
return [input stringValue];
} else if(button == NSAlertAlternateReturn){
return nil;
} else {
NSAssert1(NO,@Invalid input dialog button%d,button);
return nil;
}
}
In my application, I want to create a dialog box with one text field, and a button, through which i can prompt user and get back user entered value.
How to do this in cocoa, Objective C ?
I didn`t find any predefined method for that.
解决方案
You can call an NSAlert and put the NSTextField as it's accessoryView like this"
- (NSString *)input: (NSString *)prompt defaultValue: (NSString *)defaultValue {
NSAlert *alert = [NSAlert alertWithMessageText: prompt
defaultButton:@"OK"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@""];
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:defaultValue];
[input autorelease];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
[input validateEditing];
return [input stringValue];
} else if (button == NSAlertAlternateReturn) {
return nil;
} else {
NSAssert1(NO, @"Invalid input dialog button %d", button);
return nil;
}
}
这篇关于如何创建和获取返回值从Cocoa对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文