0

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

当标签文本是 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
4

1 回答 1

0

看起来你缺少一些大括号。if 语句或其 else 子句只需要一个“命令”,除非您将批次包装在{}. 例如

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"];
}
于 2010-11-09T04:35:00.380 回答