从 xml 创建 gui 时出现问题 ->奇怪的 CPButton 行为

发布于 2024-08-30 20:49:42 字数 2346 浏览 6 评论 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

Hallo,

I'm new to objective-j and cappuccino and just have tried to create a
small application, that creates the gui dynamically from a xml file.

Unfortunately it works only partially. It seems that the button
regions are disorder. This means, that the buttons also response if
I click besides the button....

Please help me. I dont get it..

- (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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-09-06 20:49:42

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

您可以使用 CPURLConnection 加载文件。

还有 Atlas (或 Interface Builder 和 nib2cib)将自动为您创建 cib 文件,Cappuccino 本身已经知道如何从 cib 文件构建其 UI。

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

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

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

Cappuccino gives you most of this functionality for free.

You can load files by using a CPURLConnection.

Also Atlas (or Interface Builder and nib2cib) will automatically create cib files for you, Cappuccino itself already knows how to build up it's UI from a cib files.

If you really want to implement your own system to do this, could you please provide the actual XML you are trying to load? Also try loading the button without using the XML. For example:

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

+ buttonWithTitle: will automatically call - sizeToFit on the initialized button, so you can just add it to your contentView and it should be visible with the appropriate size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文