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];
Related
I am using the below code for UIAlertController in my project.
if([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0){
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Input Error"
message:#"Please enter a valid email."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Input Error"
message:#"Please enter a valid email"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[alertView show];
}
I am getting the below waring message:
Warning: Attempt to present <UIAlertController: 0x7f8da58df1f0> on <MBComplaintsViewController: 0x7f8da36454d0> which is already presenting (null)
Kindly guide me how to properly use UIAlertController using Objective C.
Thanks,
Abin Koshy Cheriyan
Yes as per #Alexander you should not be dismissing the alert controller like this explicitly.
As per an apple engineer a new window gets added each time an UIAlertController is displayed so when we go for dismissing it the window is still there though the alert controller disappears.
So there are two ways to handle this -
Way 1 - No explicit dismiss
Do not explicitly dismiss the UIAlertController, let it be done by the user
Way 2 - Use your own window
Simply create a category on UIAertController
Here is the sample code -
.h
#import <UIKit/UIKit.h>
#interface UIAlertController (MyAdditions)
#property(nonatomic,strong) UIWindow *alertWindow;
-(void)show;
#end
In .m
#import "UIAlertController+MyAdditions.h"
#import <objc/runtime.h>
#implementation UIAlertController (MyAdditions)
#dynamic alertWindow;
- (void)setAlertWindow:(UIWindow *)alertWindow {
objc_setAssociatedObject(self, #selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIWindow *)alertWindow {
return objc_getAssociatedObject(self, #selector(alertWindow));
}
- (void)show {
self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
// window level = topmost + 1
UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
self.alertWindow.windowLevel = topWindow.windowLevel + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:self animated:YES completion:nil];
}
-(void)hide {
self.alertWindow.hidden = YES;
self.alertWindow = nil;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// just to ensure the window gets desroyed
self.alertWindow.hidden = YES;
self.alertWindow = nil;
}
To show the alert controller
UIAlertCntroller *alert = ## initialisation##;
// will show the alert
[alert show];
//to dismiss
[alert hide];
[alert dismissViewControllerAnimated:YES completion:nil];
Even you can checkout one of my sample implementation here
I don't know about your problem, but you shouldn't do that
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
It will be anyway dismissed on any of your actions.
Display some alerts in my application using the UIAlertView in iOS 7.1 works perfectly in iOS 8 the alert appears, but without the buttons to cancel, OK, and others ... This causes the user can not close the alert and consequently gets stuck on this screen, having to close the application.
I tried to implement the UIAlertView and previous versions for iOS UIAlertController 8, see the code below:
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
UIAlertView *alerta = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"s000xS2", #"Alerta") message:NSLocalizedString(#"s000xS40", nil) delegate:self cancelButtonTitle:NSLocalizedString(#"s000xS34", #"Não") otherButtonTitles:NSLocalizedString(#"s000xS35", #"Sim"), nil];
[alerta show];
}else{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:NSLocalizedString(#"s000xS2", #"Alerta")
message:NSLocalizedString(#"s000xS40", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* sim = [UIAlertAction
actionWithTitle:NSLocalizedString(#"s000xS35", #"Sim")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[Util abrirSite:[[[Player sharedPlayer] emissora] site]];
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* nao = [UIAlertAction
actionWithTitle:NSLocalizedString(#"s000xS34", #"Não")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:sim];
[alert addAction:nao];
[self presentViewController:alert animated:NO completion:nil];
}
With this code I have the same problem, the buttons are not displayed in the alert, any suggestions to get around this?
Note, I'm using strings for internationalization, they usually work, already tested by placing a string directly (# "...") but it did not work.
Try this:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"ALERTA!" message:#"What will you do?" **preferredStyle:UIAlertControllerStyleAlert**];
__weak ViewController *wself = self;
UIAlertAction *nao = [UIAlertAction actionWithTitle:#"I'm doing something" ***style:UIAlertActionStyleCancel*** handler:^(UIAlertAction *action) {
__strong ViewController *sself = wself;
sself.**lbl**.text = #"You did something!"; **//the text "You did something!" gets displayed on a label(if created) named lbl**
}];
[alert addAction:nao];
[self presentViewController:alert animated:NO completion:nil];
I have implemented correctly bump's api, and added this code:
- (void) configureBump {
[BumpClient configureWithAPIKey:#"your api key" andUserID:[[UIDevice currentDevice] name]];
[[BumpClient sharedClient] setMatchBlock:^(BumpChannelID channel) {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Matched with user" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Matched with user: %#", [[BumpClient sharedClient] userIDForChannel:channel]);
[[BumpClient sharedClient] confirmMatch:YES onChannel:channel];
}];
[[BumpClient sharedClient] setChannelConfirmedBlock:^(BumpChannelID channel) {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Channel with" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Channel with %# confirmed.", [[BumpClient sharedClient] userIDForChannel:channel]);
[[BumpClient sharedClient] sendData:[[NSString stringWithFormat:#"hi"] dataUsingEncoding:NSUTF8StringEncoding]
toChannel:channel];
}];
[[BumpClient sharedClient] setDataReceivedBlock:^(BumpChannelID channel, NSData *data) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Data received" message:[NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];
NSLog(#"Data received from %#: %#",
[[BumpClient sharedClient] userIDForChannel:channel],
[NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding]);
NSString *receivedBumpData=[NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding];
if(receivedBumpData.length!=0){
CardAvailableLandscape *cardObject=[[CardAvailableLandscape alloc] init];
[cardObject bumpInsertFunction:receivedBumpData];
}
}];
[[BumpClient sharedClient] setConnectionStateChangedBlock:^(BOOL connected) {
if (connected) {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"bump Coneected" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Bump connected...");
} else {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Bump disconnected..." message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Bump disconnected...");
}
}];
[[BumpClient sharedClient] setBumpEventBlock:^(bump_event event) {
switch(event) {
case BUMP_EVENT_BUMP:{
/*UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Bump detected." message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Bump detected.");
break;
}
case BUMP_EVENT_NO_MATCH:
{
/*UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"No match." message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"No match.");
break;
}
}
}];
}
this code has been taken from the example project. the connection is established. but i can't seem to be able to send a file: I tried this tutorial:
http://appgenor.blogspot.it/2010/02/using-bumps-new-api-to-exchange-data.html
but the Bumb object can not be created. it gives me an error. the bump has not been implemented. so it is not in the sdk, i think...
bumpObject = [[Bump alloc] init]; // Bump *bumpObject;
help please!!
I looked at the link you mention and it seems a bit old. You create bump object using static function: [BumpClient sharedClient], which also calls connect for you. I am trying to play with it as well but having problems where bump is detected but I can send data. I am using an iPod and bu.mp website to test the code.
Im creating a view in Xcode 4.3 and im unsure how to specify multiple UIAlertView's that have their own buttons with separate actions. Currently, my alerts have their own buttons, but the same actions. Below is my code.
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.examplesite1.com"]];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"examplesite2.com"]];
}
}
Thanks for any help!
There is a useful property tag for UIView(which UIAlertView subclass from). You can set different tag for each alert view.
UPDATE:
#define TAG_DEV 1
#define TAG_DONATE 2
- (IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
alert.tag = TAG_DEV;
[alert show];
}
- (IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
alert.tag = TAG_DONATE;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE){ // handle the donate
}
}
easier & newer
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
// first alert...
} else {
// sec alert...
}
}
all done!
If you find it dificult to use delegate methods to differently identifying alert view Then you can also use This Category class to use completion Block for each AlertView.
Alert_ActionSheetWithBlocks
For eg.
UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:#"AlertView+Block 1" message:#"WithBlocks" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }];
UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:#"AlertView+Block 2" message:#"WithBlocks" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }];
I hope this one is the more easiest way as compare to tag + delegate method..
He's right but you need to add this:
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate
}
}
if buttonIndex==1 then you're using the FIRST otherbutton. 0 would be for cancel. But just do nothing for 0
Or you could do this (check the title name), is just another option... Mind identically titled alerts though!
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleOneGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleTwoGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
{
if([[alertView title] isEqualToString:#"titleOneGoesHere"])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.examplesite1.com"]];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"examplesite2.com"]];
}
}
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];