Like using UIAlertView on iOS. There should be a similar solution for creating a pop-up message on macOS. Tried to search but could not find anything useful.
The class you're looking for is NSAlert. Information on this class can be found in its class reference here. And here is an example of its usage:
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Some awesome message text"];
[alert addButtonWithTitle:#"OK"];
[alert runModal];
Related
I am creating a cocoa application with finder sync extension. I'm trying to open dialog box on some events of finder app.I am beginner for x code and objective c. I have tried the below code on some event..
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:#"OK"];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:#"OK"];
[alert addButtonWithTitle:#"Cancel"];
[alert setMessageText:#"Delete the record?"];
[alert setInformativeText:#"Deleted records cannot be restored."];
[alert setAlertStyle:NSWarningAlertStyle];
if ([alert runModal] == NSAlertFirstButtonReturn) {
// OK clicked, delete the record
}
But this code is just for presenting alert box.
Can anyone suggest me how do I get dialog box.
Try UIAlertView and UIAlertController or else simply use NSAppleScript
I am trying to display a UITextField based on the OK button on the alert windows. So, when the user presses the OK button, then another textbox should come where the user can enter profile name. However, based on the code below, I see that immediately after OK is clicked, it goes on saying, that "No profile name specified" as it finds the profile.txt as empty. Here is piece of code, which displays the popup alert, prompting the user to save a profile with profile name.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello, User ! Save Profile" message:#"Provide a profile Name" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK" ,nil];
profileName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 2600.0, 25.0)];
[profileName setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:profileName];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[alert setTransform:myTransform];
[alert show];
[alert release];
Appreciate your response !
Thanks in advance !
I was able to get it working for iOS7 with,
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
profileName = [alert textFieldAtIndex:0];
Thanks
I have a NSAlert in XCode5
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Notificación"];
[alert setInformativeText:mensaje];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert beginSheetModalForWindow:window modalDelegate:nil
didEndSelector:#selector(myMethod})
contextInfo:nil];
as you see on didEndSelector I have a section for adding a method, is it possible to create there inside a custom method for making just an action, for instance something like this
[alert beginSheetModalForWindow:window modalDelegate:nil
didEndSelector:#selector(myCustomMethod{NSLog(#"hola");})
contextInfo:nil];
this is for saving the time of adding tons of methods one for each NSAlert
thanks in advance for the support
create separated method with some NSLOG for example
- (void)logs {
NSLog(#"lalalalalal")
}
Then send it to selector:
#selector(logs)
If your method receiving some variable, your select should be like:
#selector(logs:)
I have a problem regarding the UIAlertView on iOS7.
When I launch my application, it crashes with the following message:
*** Assertion failure in -[UIKeyboardTaskQueue performTask:], /SourceCache/UIKit_Sim/UIKit-2903.2/Keyboard/UIKeyboardTaskQueue.m:388
The error occurs on the following line:
- (IBAction)updatePositions:(id)sender{
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show]; <====== IT CRASHS HERE
[NSThread detachNewThreadSelector:#selector(updateDataThread) toTarget:self withObject:nil];
}
I'm using ARC and the property _alert is set defined as: #property (nonatomic,strong)
This error seems strange, because on iOS6 the code works perfectly and I don't know what should be different on iOS7.
Does anyone have an idea what could the error?
Thanks in advance.
I encountered the same error, and the issue was that the UIAlertView was attempting to be shown from a thread which wasn't the main thread.
The crash however wouldn't always occur, only when a first AlertView was already being shown while this second AlertView was trying to pop up as well.
In my case, a simple fix was to do:
//Your code here
...
//Alert
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
dispatch_async(dispatch_get_main_queue(), ^{
//Show alert here
[_alert show];
});
//Resume your code here
...
I just had this problem after forgetting that I was working from a background thread. I don't know if that's the case here, but I'd make sure you're not trying to call updatePositions: from anything other than the main thread.
Change your code like this :
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show];
removing [[ and ]] around #"text"
But, your I don't think your problem came from this UIAlertView.
I had the same problem as well but not too familiar with the method dispatch_async. I used
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
and the problem hasn't come up again.
Put your alertview code in a separate function like
-(void)showAlert
{
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show];
}
Then in your IBAction do this
- (IBAction)updatePositions:(id)sender
{
[self performSelectorOnMainThread:#selector(showAlert) withObject:nil waitUntilDone:YES];
[NSThread detachNewThreadSelector:#selector(updateDataThread) toTarget:self withObject:nil];
}
You can also do like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Your title" message:#"Your message" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
However, if you need to show the same alert in multiple places it's better to create a separate function for it.
I'm trying to trigger an alert with an if statement. but the code im using wont do what i want.
This is one attempt at triggering the alert
-(Void)ShowAlert
if (mainInt == 120) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Message"
delegate:nil cancelButtonTitle:#"Dismiss"otherButtonTitles:nil, nil];
[alert show];
}
i have declared 'ShowAlert' in the .h file as -(void)ShowAlert just under my -(IBAction) declares.
But still no alert when ran in simulator. Any help with this would be greatly appreciated!!
THANKS
(I'm am using Xcode 4.6.2)
Maybe if (mainInt == 120) is never true.
I would try with an ELSE if i were you. And log whole process about what's going on like this:
-(void)ShowAlert{
if (mainInt == 120){
NSLog(#"TRUE i should see the alertview! , mainInt = %i",mainInt);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Message"
delegate:nil cancelButtonTitle:#"Dismiss"otherButtonTitles:nil, nil];
[alert show];
}else{
NSLog(#"FALSE , mainInt = %i",mainInt);
}
}