问题
Ok. here is the situation, I am an average c++ developer. I do not have any prior experience of working with Objective C or Cocoa for that matter.
I am working on a project currently in OSX with Carbon [I am NOVICE in carbon as well], and been coding in pure C++ for 3 months.
Now I am facing an issue where I have to display an input box to the user, and get some input from him, say USERNAME, having literally no knowledge of how input boxes are displayed in OSX, what are my options. I have done programming in win32, so, a couple of hours of reading and 1 hour of programming should do the job for me. I just need a little help pointing me in right direction.
Here is what I have got from Googling a little bit--
There are 3 ways which can be used to implement an input box in OSX
1- Use carbon, I have been able to display a simple dialog box using it. I don't know how I can use input boxes there.. Here is the code I tried for Input Box..
DialogRef dialog = GetNewDialog (128, NULL, (WindowRef)-1);
WindowRef lay;
ControlRef outControl;
Rect boundsRect;
boundsRect.top = 0;
boundsRect.left = 0;
boundsRect.right = 200;
boundsRect.bottom = 99;
lay = GetDialogWindow(dialog);
CreateEditTextControl (lay, &boundsRect, NULL, false, true, NULL, &outControl);
InstallStandardEventHandler(GetWindowEventTarget (lay));
ShowWindow (lay);
I was not able to see anything when I ran the program, and Xcode showed a warning on
CreateEditTextControl
saying it is deprecated.
Option 2 which I have is to combine Objective C and C++, but I do not know how objective C works, here is a little lead I got in doing this. I only have a few hours to accomplish this.
Option 3 I found this here.
//
// test1.cpp
// This program shows how to access Cocoa GUI from pure C/C++
// and build a truly functional GUI application (although very simple).
// Compile using:
// g++ -framework Cocoa -o test1 test1.cpp
//
// that will output 'test1' binary.
//
#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <iostream>
extern "C" int NSRunAlertPanel(CFStringRef strTitle, CFStringRef strMsg,
CFStringRef strButton1, CFStringRef strButton2,
CFStringRef strButton3, ...);
int main(int argc, char** argv)
{
id app = NULL;
id pool = objc_getClass("NSAutoreleasePool");
if (!pool)
{
std::cerr << "Unable to get NSAutoreleasePool!\nAborting\n";
return -1;
}
pool = objc_msgSend(pool, sel_registerName("alloc"));
if (!pool)
{
std::cerr << "Unable to create NSAutoreleasePool...\nAborting...\n";
return -1;
}
pool = objc_msgSend(pool, sel_registerName("init"));
app = objc_msgSend(objc_getClass("NSApplication"),
sel_registerName("sharedApplication"));
NSRunAlertPanel(CFSTR("Testing"),
CFSTR("This is a simple test to display NSAlertPanel."),
CFSTR("OK"), NULL, NULL);
objc_msgSend(pool, sel_registerName("release"));
return 0;
}
回答1:
All of Carbon UI is deprecated, and cannot be used for 64-bit development.
A 3rd alternative would be CFUserNotification.
回答2:
Using option2 : Cocoa framework provides you all sets of simple controls, messagbox and others. But there are no inputBox. :(
However you can create your custom inputBox or modify the alertpanel as I did here:
- (NSString *)inputBox: (NSString *)prompt{
NSAlert *alert = [NSAlert alertWithMessageText: prompt
defaultButton:@"OK"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@""];
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
[input validateEditing];
return [input stringValue];
}
else if (button == NSAlertAlternateReturn) {
return nil;
}
else {
return nil;
}
}
来源:https://stackoverflow.com/questions/20392802/how-to-display-an-input-box-in-mac-osx-using-c