NSAlert Box Not Displaying

I am working on my first cocoa / Objective-C application, so please bear with me if I am doing something obviously wrong. I have an application configured to copy everything that is in an NSTextField in a window to another NSTextField (in this case, a shortcut). If the user has not entered anything in the text field, he should display a warning, but this is not so. What is wrong with my code?

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
}

Also, are there any guides for the methods used by Cocoa UI elements (e.g. naming schemes)? I use the .NET style of GUI programming. @end

+5
source share
2 answers

:

if(_textBox1.stringValue != @"")

, true, @"" , .

:

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

:

if (_textBox1.stringValue.length > 0)

+10

? beginSheetModalForWindow:

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

All Articles