2011-12-14 19 views
5

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

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 
} 

また、(スキーム命名のような)ココアUI要素によって使用される方法のいずれかのガイドがありますか?私は、GUIプログラミングの.NETスタイルを使用しています。 @end

+0

、ココアの命名/コーディングガイドラインdeveloper.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/ –

答えて

10

はあなたの問題です:

if(_textBox1.stringValue != @"")

あなたはポインタの平等を比較しているので、一定の@""文字列がテキストフィールドの文字列と同じオブジェクトになることはありませんので、この式は常にtrueを返します。オブジェクト。

この比較を行うための正しい方法は次のようになります。

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

かさえ良くします。http://あなたの2番目の質問については

if (_textBox1.stringValue.length > 0)

0

警告をモーダルで実行しようとしましたか?ここでbeginSheetModalForWindow:

[msgBox beginSheetModalForWindow:self.window 
        modalDelegate:self 
        didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) 
        contextInfo:nil]; 
+0

まだ表示されていません。 – airplaneman19

関連する問題