首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在if/else语句中更改CPButton中的标题(Objective-J)

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

Stack Overflow用户
提问于 2010-11-09 11:58:34
回答 1查看 179关注 0票数 0

我有一个非常简单的Objective-J webapp,是一个标签和一个按钮。当按下按钮时,标签文本会发生变化。我还想更改按钮的标题。如果我将更改语句放在下面的交换函数(按钮被按下时运行的函数)中,那么webapp就不能正常启动。

我如何修改这段代码,使按钮在标签文本为Good Bye Tommy时显示为Tommy Arrives?

代码语言:javascript
复制
@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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-09 12:35:00

看起来你少了一些牙套。一个if语句或它的else子句只接受一个‘命令’,除非你把它包装在{}中。例如。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4130400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档