2010-11-09 50 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