0

你好,

我是 Objective-j 和卡布奇诺的新手,只是尝试创建一个小应用程序,它从 xml 文件动态创建 gui。

不幸的是,它只能部分起作用。按钮区域似乎是无序的。这意味着,如果我在按钮旁边单击,按钮也会响应......

请帮我。我不明白。。

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{

    mControlList = [CPArray alloc];

   theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero()
    styleMask:CPBorderlessBridgeWindowMask],
    contentView = [theWindow contentView];
    [contentView setFrame:[[contentView superview] bounds]];
    [contentView setAutoresizingMask:CPViewWidthSizable |
CPViewHeightSizable];


    // Loadxmlfile
    var xhttp;
    if (window.XMLHttpRequest)
    {
        xhttp=new XMLHttpRequest()
    }
    else
    {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    xhttp.open("GET","test.xml",false);
    xhttp.send("");
    xmlDoc = xhttp.responseXML;

    //Get controls nodeand iterate through all controls
    var node = xmlDoc.getElementsByTagName("controls")[0];
    for (var i=0; i<node.childNodes.length; i++) {
        if(node.childNodes[i].nodeName=="button"){
            var item = node.childNodes[i];

            var name = item.attributes["name"].nodeValue;
            var text = item.getElementsByTagName("text")
[0].childNodes[0].nodeValue;
            var x=      item.getElementsByTagName("rect")
[0].attributes["x"].nodeValue;
            var y=      item.getElementsByTagName("rect")
[0].attributes["y"].nodeValue;
            var width=  item.getElementsByTagName("rect")
[0].attributes["width"].nodeValue;
            var height= item.getElementsByTagName("rect")
[0].attributes["height"].nodeValue;

            var b = [[Button alloc] InitWithParent:contentView Text:text X:x
Y:y Width:width Height:height];
            [mControlList addObject:b];
        }
    }

    [theWindow orderFront:self];

}

@implementation Button : CPObject
{
    CPButton _button;
}

- (Button)InitWithParent:(CPView)contentView Text:(CPString)text X:
(int)x Y:(int)y Width:(int)width Height:(int)height
{
    _button = [[CPButton alloc] initWithFrame:
CGRectMake(x,y,width,height)];
    [_button setTitle:text];
    [_button setTarget:self];
    [_button setAction:@selector(cmdNext_onClick:)];
    [contentView addSubview:_button];
    return self;
}

- (void)cmdNext_onClick:(id)sender
{
}
@end
4

1 回答 1

0

卡布奇诺免费为您提供大部分功能。

您可以使用CPURLConnection加载文件。

Atlas(或Interface Builder和nib2cib )也会自动为您创建 cib 文件,Cappuccino 本身已经知道如何从 cib 文件构建它的 UI。

如果您真的想实现自己的系统来执行此操作,您能否提供您尝试加载的实际 XML?也尝试在不使用 XML 的情况下加载按钮。例如:

var myButton = [CPButton buttonWithTitle:@"My Cool Button"];
[contentView addSubview:myButton];

+ buttonWithTitle:将自动调用- sizeToFit初始化按钮,因此您只需将其添加到您的 contentView 中,它应该以适当的大小可见。

于 2010-05-10T09:54:05.500 回答