5

私は初めてのcocoa/Objective-Cアプリケーションに取り組んでいるので、明らかに間違ったことをしている場合はご容赦ください。ウィンドウのNSTextFieldにあるものを別のNSTextField(この場合はラベル)にコピーするようにアプリケーションを設定しました。ユーザーがテキストボックスに何も入力していない場合は、アラートが表示されますが、表示されません。私のコードの何が問題になっていますか?

AppDelegate.m:

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize textBox1 = _textBox1;
@synthesize label1 = _label1;

- (void)dealloc
{
 [super dealloc];
}

-(IBAction)setLabelTxt: (id)sender{

    if(_textBox1.stringValue != @"")
        [_label1 setStringValue: _textBox1.stringValue];
    else{
        NSAlert* msgBox = [[[NSAlert alloc] init] autorelease];
        [msgBox setMessageText: @"You must have text in the text box."];
        [msgBox addButtonWithTitle: @"OK"];
        [msgBox runModal];
        }
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

また、Cocoa UI要素(命名スキームなど)で使用されるメソッドのガイドはありますか?私はGUIプログラミングの.NETスタイルを使用しています。@終わり

4

2 に答える 2

10

これがあなたの問題です:

if(_textBox1.stringValue != @"")

ポインタの等価性を比較しているtrueため、文字列定数@""がテキストフィールドの文字列オブジェクトと同じオブジェクトになることはないため、この式は常に返されます。

この比較を行う正しい方法は次のとおりです。

if (![_textBox1.stringValue isEqualToString:@""])

またはさらに良い:

if (_textBox1.stringValue.length > 0)

于 2011-12-14T02:59:55.777 に答える
0

アラートをモーダルで実行してみましたか?beginSheetModalForWindow:

[msgBox beginSheetModalForWindow:self.window
                   modalDelegate:self 
                  didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)  
                     contextInfo:nil];
于 2011-12-14T02:36:43.073 に答える