在if / else语句中更改CPButton中的标题(Objective-J)

时间:2010-11-09 03:58:34

标签: javascript javascript-framework objective-j

我有一个非常简单的Objective-J webapp,它是一个标签和一个按钮。按下按钮时标签文本会发生变化。我想让按钮的标题也改变。如果我将更改语句放在下面的交换函数中(按下按钮时运行的函数),则webapp无法正常启动。

如何修改此代码,以便在标签文字为Good Bye Tommy时按钮读取Tommy Arrives?

@import <Foundation/CPObject.j>


@implementation AppController : CPObject
{
    CPTextField label;
    CPButton button;
}

// Launches UI in the App
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
    var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],contentView = [theWindow contentView];

label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];

[label setStringValue:@"Hello Tommy Jones!"];
[label setFont:[CPFont boldSystemFontOfSize:24.0]];

[label sizeToFit];

[label setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[label setCenter:[contentView center]];

[contentView addSubview:label];
[label setAlignment:CPCenterTextAlignment]; 

button = [[CPButton alloc] initWithFrame: CGRectMake( CGRectGetWidth([contentView bounds])/2.0 - 50, CGRectGetMaxY([label frame]) + 10, 100, 24 )]; 

[button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[button setTitle:"Tommy Leaves"];
[button setTarget:self];
[button setAction:@selector(swap:)];
[contentView addSubview:button];

[theWindow orderFront:self];

// Uncomment the following line to turn on the standard menu bar.
[CPMenu setMenuBarVisible:YES];
}


// Executes when button is pressed
- (void)swap:(id)sender 
{ 
    if
    ([label stringValue] == "Hello Tommy Jones!") [label setStringValue:"Good Bye Tommy!"];
    // [button setTitle:"Tommy Leaves"];
    // [contentView addSubview:button];

        else
    [label setStringValue:"Hello Tommy Jones!"];
    // [button setTitle:"Tommy Arrives"];
}  

@end

1 个答案:

答案 0 :(得分:0)

看起来你错过了一些大括号。 if语句或其else子句只接受一个“命令”,除非您将该批包装在{}中。 E.g。

if ([label stringValue] == "Hello Tommy Jones!") 
{
    [label setStringValue:"Good Bye Tommy!"];
    [button setTitle:"Tommy Leaves"];
}
else
{
    [label setStringValue:"Hello Tommy Jones!"];
    [button setTitle:"Tommy Arrives"];
}