ok button for alert in mac app does not work - macos

On a button click, I am using the below code
testViewController *myWindowController = [[testViewController alloc] initWithWindowNibName:#"RecordingsViewController"];
[myWindowController setDelegate:self];
activeModalWindow = [myWindowController window];
[NSApp beginSheet:[myWindowController window]
modalForWindow:[self window]
modalDelegate:self
didEndSelector:#selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:nil];
[NSApp runModalForWindow:[myWindowController window]];
[[myWindowController window] orderOut: self];
From this testViewController, I am showing an alert using the code
NSString *theAlertMessage = [NSString stringWithFormat: #"Already added"];
NSRunAlertPanel(#"", theAlertMessage, #"OK", nil, nil);
But on clicking ok button of this alert. My alert remains on screen. Please help!

Use this to as :
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:2];
[alert setMessageText:#"Already added"];
[alert beginSheetModalForWindow:[(AppDelegate *)[[NSApplication sharedApplication] delegate] window]
modalDelegate:self
didEndSelector:#selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}
- (void)sheetDidEnd:(NSAlert *)alert
returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
NSLog(#"clicked %d button\n", returnCode);
}
Hope it helps you.

Found an alternative, it works perfectly
NSString *theAlertMessage = [NSString stringWithFormat: #"Already added."];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert addButtonWithTitle:#"OK"];
[alert setMessageText:theAlertMessage];

Related

How to close an alert after some time and repeat it every 10 min

In my MAC OSX application. I am throwing a an alert pop up asking user to select yes or no. if user doesnt click any of the choices and may drag it to some corner. So i wanted to autoclose it after some time and again show the same alert. so i can ensure him to take same action.
Alert code i am using is
-(bool)VpnStatusUnableToConnect:(NSString *)alertMessage
{
if (nil != alertMessage) {
NSImage *alertIcon = [NSImage imageNamed:#"dock-alert"]; //my custom image placed in support files
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:#"Try Again"];
[alert addButtonWithTitle:#"Cancel"];
[alert setMessageText:alertMessage];
[alert setAlertStyle:NSWarningAlertStyle];
[alert setIcon:alertIcon];
[[alert window] setTitle:#"VPN Connection Status"];
[[alert window] setBackgroundColor: NSColor.whiteColor];
if ( [alert runModal] == NSAlertFirstButtonReturn)
{
return 1;
}
else
return 0;
}
return 0;
}
Modify your code as below and give a try
-(void)yourAlert{
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: #"OK"];
[alert setMessageText: #"Attention!!! This a critical Alert."];
[alert setAlertStyle: NSInformationalAlertStyle];
NSTimer *myTimer = [NSTimer timerWithTimeInterval:3
target:self
selector: #selector(killWindow:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSModalPanelRunLoopMode];
int choice = 0;
choice = [alert runModal];
if(choice != 0)
[myTimer invalidate];
}
-(void) killWindow:(NSAlert *)alert with:(NSTimer *) theTimer;
{
NSLog(#"killWindow");
[[alert window] abortModal];
}

Dialog like Xcode in OS X

I want to show the dialog with text input as sheet below.
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) {
}];
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;
}
}

Posting via SLComposeViewController on facebook and twitter in IOS-8

I have tried to post on facebook and twitter using SLComposeViewController. my code is
-(void)postToFacebookWithObject:(id)object FromController:(UIViewController*)vc {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled)
{
DLog(#"Cancelled");
}
else
{
DLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller removeAllImages];
[controller removeAllURLs];
NSMutableDictionary *item = (NSMutableDictionary *)object;
[controller setInitialText:[item objectForKey:#"DealTitle"]];
if([item objectForKey:#"DealImage"])
[controller addImage:[item objectForKey:#"DealImage"]];
if([item objectForKey:#"url"])
[controller addURL:[NSURL URLWithString:[item objectForKey:#"url"]]];
[vc presentViewController:controller animated:YES completion:Nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:LocStr(#"NO_FACEBOOK_ACCOUNT_CONFIGURED") delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
DLog(#"UnAvailable");
}
}
this working fine in ios 7 but in ios 8 this sheet coming behind my view which i have added on window. How i can fix this?
I have solve this issue by adding my view on navigationcontroller.view not on window.
I have resolved through open present-view controller in navigation-bar.it may help you.
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:shareFrom];
[controller addImage:self.photoImageView.image];
[controller addURL:[NSURL URLWithString:dayCareWebsite]];
dispatch_async(dispatch_get_main_queue(), ^ {
[self.navigationController presentViewController:controller animated:YES completion:nil];
});
UINavigationController *forShare = [[UINavigationController alloc] initWithRootViewController:vc];
[forShare setNavigationBarHidden:YES];
[self presentViewController:forShare animated:NO completion:nil];
if animated:YES it doesn't works
if animated:NO it works for me

iOS8 AlertViewController issue

I have one textField in my viewController, when AlertViewController is appear keyboard is not appear at in background of the AlertViewController. But After dismiss AlertViewController Keyboard is appear. This thing happen only for ios8 device. Here is my following Code:
[textField becomeFirstResponder];
if([self checkIfiOS8])
{
// for iOS 8
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Your Title"
message:#"Your message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
// iOS7
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Your Title" message:#"Your message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"AlertView" message:#"I am an AlertView" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

UIActivityViewController

I have a problem with the UIActivityViewController on iPad
Here is my Code:
NSString* someText = #"Hi, I am using the official iEveryThing Tech app #iEveryTech";
NSString* Website = [NSURL URLWithString:#"https://itunes.apple.com/us/app/ieverytech/id578148847?l=de&ls=1&mt=8"];
NSArray* dataToShare = #[someText,Website];
Class activityViewController = NSClassFromString(#"UIActivityViewController");
if(activityViewController) {
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];}
}
On iPhone the Mail composer View will disappear after sending the mail but not the Composer on the iPad here is my Composer:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error"
message:[NSString stringWithFormat:#"error %#", [error description]]
delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
else {
[self dismissModalViewControllerAnimated:YES];
}
}
The activityViewController use a native method for send mail don't need use mailComposeController, like this
NSMutableArray *items = [NSMutableArray new];
[items addObject:#"text for share"];
[items addObject:[UIImage imageWithData:[NSData dataWithContentsOfFile:self.imagePath]]];
NSArray *activityItems = [NSArray arrayWithArray:items];
self.activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:[NSArray arrayWithObjects:whatsapp,instagram, nil]];
self.activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePrint,UIActivityTypeAirDrop,UIActivityTypeCopyToPasteboard];
[self presentViewController:self.activityVC animated:YES completion:nil];
First thing, when using the activityViewController, your class is not the delegate for the mail composer and the code will not be called. This is all handled internally in the activityViewController.
As for your view not being dismissed, implement the complete block call for the activityViewController. In the block be sure to dismiss the view controller.

Resources