MacOS NSAlert

This is an old revision of MacOS NSAlert, edited by Trev at . The current revision is 142141.

/* Modal error dialog with OK button */ Add FPC version

This article applies to macOS only.

See also: Multiplatform Programming Guide

Overview

The NSAlert class allows you to specify an alert level, alert text, button titles, and a custom icon if required. The class also lets your alerts display a help button and provides ways for applications to offer help specific to an alert. The NSAlert class is available from Mac OS X Panther 10.3 onwards. An NSAlert object is intended for a single alert with a unique combination of title, buttons, and so on that is displayed on a particular condition. You should create an NSAlert object for each alert dialog, creating it only when you need to display it, and releasing it when you are done.

Code examples

under construction

This example demonstrates a simple modal error dialog with just error message text and an OK button to exit the dialog. Compiles with FPC 3.0.4 onwards.

unit Unit1;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}

interface

uses
  Forms,    // needed for Form1
  StdCtrls, // needed for Button1
  CocoaAll; // needed for NSAlert, NSStr

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private

  public

  end;


var
  Form1: TForm1;

implementation

{$R *.lfm}

{Display error message modal dialog with OK button.}
procedure ErrorModalDlgWithOKbutton(const ErrorMsg : NSstring);
var
  Alert : NSAlert;
begin
  Alert := NSAlert.alloc.init;
  Alert.setInformativeText(ErrorMsg);
  Alert.runModal;
  Alert.release;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  ErrorModalDlgWithOKbutton(NSStr('Error Message Test!'));
end;

end.

See also