【发布时间】:2013-12-21 23:14:58
【问题描述】:
好的。情况就是这样,我是一名普通的 C++ 开发人员。我之前没有任何使用 Objective C 或 Cocoa 的经验。
我目前正在使用 Carbon 在 OSX 中开展一个项目 [我也是 carbon 方面的新手],并且已经使用纯 C++ 进行了 3 个月的编码。
现在我面临一个问题,我必须向用户显示一个输入框,并从他那里得到一些输入,比如 USERNAME,实际上不知道输入框在 OSX 中是如何显示的,我有什么选择。我已经在 win32 中完成了编程,因此,几个小时的阅读和 1 小时的编程应该可以为我完成这项工作。我只需要一点帮助,为我指明正确的方向。
这是我从谷歌上搜索到的一点点--
在 OSX 中可以使用 3 种方式来实现输入框
1- 使用碳,我已经能够使用它显示一个简单的对话框。我不知道如何在那里使用输入框.. 这是我为输入框尝试的代码..
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);
我在运行程序时看不到任何东西,并且 Xcode 在
CreateEditTextControl 说它已被弃用。
选项 2 我拥有的是结合 Objective C 和 C++,但我不知道 Objective C 是如何工作的,这是我在做 this 时得到的一点线索。我只有几个小时来完成这项工作。
选项 3 我找到了这个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;
}
【问题讨论】:
标签: c++ xcode macos cocoa macos-carbon