代码之家  ›  专栏  ›  技术社区  ›  Andrew Lauer Barinov

在if/else语句中更改CPButton中的标题(Objective-J)

  •  0
  • Andrew Lauer Barinov  · 技术社区  · 15 年前

    如何修改此代码,以便在标签文本“再见”时按钮读取Tommy到达?

    @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
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   Alexander Ljungberg    15 年前

    你好像少了一些牙套。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"];
    }
    
    推荐文章