Search code examples
objective-ccocoaalertnspanel

Alert above a NSPanel?


I have a NSPanel. I use the following code:

[NSApp beginSheet:<NSPanel>
       modalForWindow:[[self view] window]
        modalDelegate:self 
       didEndSelector:nil 
          contextInfo:nil];

This Panel has a view that contains a NSTextField. For some condition with NSTextField text, i need to display an Alert on top of the NSPanel View. Code for throwing Alert:

NSBeginAlertSheet(@"OK", @"Cancel", nil,nil, [[self view] window], self, nil,  nil,nil, @"Error");

Any idea how can i achieve this?


Solution

  • Got the solution. Need to override NSControlTextEditingDelegate method with the following:

    - (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error
    
    {
           NSBeginAlertSheet(@"OK", @"Cancel", nil,nil, <NSPanel instance>, self, nil,  nil,nil, @"Error");
    
    }
    

    :) :)