NSAlert - Retrieve Button Selected Field?

After signifacant research on this question, I need an expert's help! I know how to determine which button was selected and pass it to the caller. This seems inefficient to me, because 1) I have to determine which button was selected, 2) pass an appropriate response to caller, and 3) caller has to determine the result returned.


I want to move the test in the NSAlert function for button selected, and let the caller make this detrmination. What does NSAlert need to pass to its caller for the caller to make this test? I'd like to remove one of the tests.

It would probably be easier to understand your question with some code.


When you say :

I have to determine which button was selected :

do you speak of the button in NSAlert ? Which are your alert buttons ?


pass an appropriate response to caller:

what type of response do you need ? A String ? A value ?


caller has to determine the result returned.:

what do you mean ?

Do you need to get the NSAlert button pressed (I suppose, no, because tha's obvious) or the message that the NSAlert has posted ?

In the latter case, you can store it in a var that is visible from the caller and the alert..

Not sure if I understand you question clearly. If you use runModal method, you only get the

NSModalResponse
code. But with beginSheetModalForWindow:completionHandler: you can do whatever you want based on the response code on completion.


But beware of the response code depending on how you created NSAlert instance:

<quote>

Special Considerations


If you use alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat: to create an alert, the following constants are used to identify the button used to dismiss the alert: NSAlertDefaultReturn, NSAlertAlternateReturn, and NSAlertOtherReturn. Otherwise, the constants used are the ones described in Button Return Values.

</quote>

lHow do I copy code and paste in my original question or any replies. I had previously been told this won't work in the Apple Forums.


In my original question, I'm using runModal method. In the method, I know which button was clicked (Add, Edit, Delete or Cancel) based on NSAlertFirstButtonReturn, NSAlertFSecondButtonReturn, etc. Rather than test for the button selected, I want to return "the result of the button selection" to the caller so it can make the test. Otherwise, I have to make the test and pass a result to the caller, so the caller can test to see what was returned. This is redundant!

copy and paste works for text, not for images.


When you copy code, you should also format it : select the code in the post and clicxk on <> icon at the top of the edit field.


BTW, having a redundant test is not really an issue, but, to achieve what you want :

if you call runModal like this in the caller:


let modalResponse = alert.runModal()


then you know what was clicked in the NSAlert.


Is this what you need ?

NSAlert - Retrieve Button Selected Field?
 
 
Q