Kirit  Vaghela
Kirit Vaghela

Reputation: 12674

Dialog like Xcode in OS X

I want to show the dialog with text input as sheet below.

enter image description here

I try with NSAlert but i don't want to show app icon in dialog.

NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:kAppTitle];
[alert setInformativeText:kMsgSetDeviceName];
[alert addButtonWithTitle:kButtonOK];
[alert addButtonWithTitle:kButtonCancel];

NSString *deviceName = @"";

NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
[input setStringValue:deviceName];

[alert setAccessoryView:input];
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger button) {

}]; 

Upvotes: 1

Views: 387

Answers (1)

SDS
SDS

Reputation: 211

You can use http://www.knowstack.com/nsalert-cocoa-objective-c/ link to create custom alert sheet in OSX.

-(void)showCustomSheet
{
 {
 if (!_customSheet)
 //Check the myCustomSheet instance variable to make sure the custom sheet does not already exist.
 [NSBundle loadNibNamed: @"CustomSheet" owner: self];
 [NSApp beginSheet: self.customSheet
 modalForWindow: self.window
 modalDelegate: self
 didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
 contextInfo: nil];
 // Sheet is up here.
 }
}
- (IBAction)closeMyCustomSheet: (id)sender
{
 [NSApp endSheet:_customSheet];
}
- (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
 NSLog(@"%s",__func__);
 NSLog(@"return Code %d",returnCode);
 [sheet orderOut:self];
}

The below method is required to have a different point to show the alert from

- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet
 usingRect:(NSRect)rect
{
 NSLog(@"%s",__func__);
 if (sheet == self.customSheet)
 {
 NSLog(@"if block");
 NSRect fieldRect = [self.showAlertButton frame];
 fieldRect.size.height = 0;
 return fieldRect;
 }
 else
 {
 NSLog(@"else block");
 return rect;
 }
}

Upvotes: 1

Related Questions