2010-05-01 40 views
0

喂,问题从XML创建图形用户界面 - >奇怪CPButton行为

我新的目标-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 
+0

您的XML阅读器不会生成字符串而不是整数吗?我会测试没有XML部分的按钮代码。 – 2010-05-04 18:11:18

+0

Thx,我只是修正了它......你是对的,字符串值必须在传递给CGRectMake之前转换为数字。令人困惑的是,无论如何,该按钮被正确绘制,但只有鼠标事件不起作用... – Superpro 2010-05-09 14:55:50

回答

0

卡布奇诺给你最该功能是免费的。

您可以使用CPURLConnection加载文件。

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

如果你真的想要实现你自己的系统来做到这一点,你能提供你正在尝试加载的实际XML吗?也请尝试加载按钮而不使用XML。例如:

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

+ buttonWithTitle:会自动调用的初始化按钮- sizeToFit,所以你可以把它添加到您的内容查看,它应该是可见用适当的大小。