0

ラベルとボタンである非常に単純なObjective-JWebアプリがあります。ボタンを押すとラベルのテキストが変わります。ボタンのタイトルも変えたいです。以下のスワップ関数(ボタンが押されたときに実行される関数)にchangeステートメントを入れると、Webアプリが正しく起動しません。

ラベルのテキストがGoodByeTommyの場合に、ボタンに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 に答える