2010-11-09 52 views
0

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

如何修改此代码,使标签文字为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 

回答

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"]; 
} 
+0

感谢您的帮助! – 2010-11-09 22:57:14