【问题标题】:Changing title in CPButton within an if/else statement (Objective-J)在 if/else 语句中更改 CPButton 中的标题 (Objective-J)
【发布时间】:2011-05-07 00:50:48
【问题描述】:

我有一个非常简单的 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

【问题讨论】:

    标签: javascript javascript-framework objective-j


    【解决方案1】:

    看起来您缺少一些大括号。 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"];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2021-02-20
      相关资源
      最近更新 更多