At the end of a process of doing backup I want to let the user know the backup is complete. I used to use UIAlertView, which works. However its depreciated so was swapping out these for UIAlertController. After the popup message is complete the window closes. The new UIAlertController doesnt seem to work in this situation. What am I doing wrong?
This is happening and the END of the process right before the view closes. The last line in the code below is closing the view. UIAlertView must be modal or something that stops the view from closing?
I used to use this code and it worked great.
UIAlertView *myalert = [[UIAlertView alloc] initWithTitle:nil message:Msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[myalert show];
[self presentViewController:alert animated:YES completion:nil];
And this new code you dont see anything
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#""
message:Msg
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
This line looks wrong:
[self dismissViewControllerAnimated:NO completion:nil];
Try removing that or putting it somewhere more suitable. I'd suggest that you are immediately presenting and then dismissing the alert.
[self dismissViewControllerAnimated:NO completion:nil];
Remove this line as you are presenting alert and quickly dismissing the View in which the alert was presented.
In short you are showing alert and removing the view on which the alert was shown
Related
I currently have a UIAlertController that I need to make non-dismissible. The Alert should not dismiss when pressing the action button.
How can I do this?
UIAlertController *alert;
int bestScore = [[[NSUserDefaults standardUserDefaults] objectForKey:#"bestScore"] intValue];
if (!bestScore || bestScore < _score){
[[NSUserDefaults standardUserDefaults] setObject:#(_score) forKey:#"bestScore"];
alert = [UIAlertController alertControllerWithTitle:#"GAME OVER "
message:[NSString stringWithFormat:#"NEW RECORD! \n SCORE : %d \n\n\n\n\n\n", _score] preferredStyle:UIAlertControllerStyleActionSheet];
}
else alert = [UIAlertController alertControllerWithTitle:#"GAME OVER"
message:[NSString stringWithFormat:#"SCORE : %d \n Best score : %d \n\n\n\n\n\n ", _score, bestScore] preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:#"Try again" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self newGame];
[self addNewView];
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Answer" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[alert viewWillDisappear:NO];
}]];
[self presentViewController:alert animated:YES completion:nil];
I would recommend creating a UIViewController that contains a UIView. Within this UIView you will be able to display your required information and add the custom button actions that you desire.
In order to make the UIView appear like a modal view controller add a UIVisualEffectView with a UIBlurEffectStyle.
This is as simple as creating a conventional view controller using your storyboard/xib adding the required UIView in the interface builder then linking up the associated view controller class. After completing the initial setup and user interface add the following code to viewDidLoad. Furthermore you can perform the required animations etc within viewWillAppear & viewWillDisappear.
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]
UIVisualEffectsView *blurView = [[UIVisualEffectView alloc] initWithEffect:effect];
[self.view insertSubview:blurView atIndex:0];
You can do like this
If you dont want dismiss alertController then You can disabled button actions of it like
alert.actions[1].enabled = NO
This will make your alertController Non-dismissable.
simply present the alertContoller again at the start of the action for alertButton you want it to not dismiss,
[self presentViewController:alertController animated:YES completion:nil];
With the latest iOS 8.3 release, our app starts to have a weird behavior.
After finishing textfield editing, the user can click the close button which brings up an UIAlertView. When the user clicks discard in the alertview, alertview and current view are dismissed. But somehow the keyboard shows up after the view is gone which is quite annoying to users.
After some debugging, it seems that the keyboard is shown for the last textfield that the user has accessed before closing the view. I tried various ways to endEditing for the current view in many places (before showing UIAlertView, after clicking a button in the UIAlertView; I even set the focus to another UI element of the view). It didn't solve the problem.
But for this particular issue, I'm not sure if it's a common issue or something we need to fix. Everything works perfectly before iOS 8.3.
We understand that UIAlertView is deprecated for iOS 8. We're starting to migrate to UIAlertController. But if there's any workaround, we'd love to hear.
Here's some code snippet.
- (IBAction)closeTapped:(UIButton *)sender
{
// try to resign first responder
// [self.tfName resignFirstResponder];
// [self.tfPosition resignFirstResponder];
[self.view endEditing:YES];
if(self.orderDetails.isOpen && self.orderItemChanged)
{
UIAlertView* saveAlert = [[UIAlertView alloc] initWithTitle:#"Unsaved Changes"
message:#"Your changes have not been saved. Discard changes?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", #"Discard", nil];
[saveAlert show];
}
else
{
[self close];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex)
{
case 1: // Save
{
[self save];
break;
}
case 2: // Discard
{
[self close];
break;
}
}
}
- (void)close
{
[self.delegate dismissEditOrderItemVC];
}
If your deployment target is iOS 8+, try UIAlertController.
Here's a quick fix for UIAlertView: delay the invocation of showing the alert view when your text field or text view resigns first responder.
[self performSelector:#selector(showAlertView) withObject:nil afterDelay:0.6];
If anyone struggles with this, I hope this will help:
if (NSClassFromString(#"UIAlertController")) {
UIAlertController* alert = ...
}
else {
UIAlertView* alert = ...
}
you need to change alert for ios 8.3
first put this in your view
#define IS_IOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0
then
if (IS_IOS8) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:#"Unsaved Changes" message:#"Your changes have not been saved. Discard changes?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *saveAction = [UIAlertAction
actionWithTitle:#"Save"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[self save];
}];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *discardAction = [UIAlertAction
actionWithTitle:#"Discard"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[alertVC dismissViewControllerAnimated:YES completion:nil];
}];
[alertVC addAction:saveAction];
[alertVC addAction:cancelAction];
[alertVC addAction:discardAction];
[self.view.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
this will help you as it helps me in same problem.
above code is compatible with both ios 7 & 8
I too, had a keyboard popping up (with the cursor in the last-used textView) after closing a UIAlertController and here is a very simple fix:
Immediately before building and presenting the UIAlertController,
Using [_activeTextView resignFirstResponder]; the keyboard will reappear.
Using [self.view endEditing:YES]; the keyboard will NOT reappear.
I hope this helps you.
Try using the below code. It works fine for iOS 8 and below version
if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
[alertVC addAction:cancelAction];
[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
}];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
If a text field is the first responder, it will automatically pop up the keyboard when the alert is dismissed. Ensure the first responder is properly dismissed with:
[textField resignFirstResponder]
Remember: in a table or scroll view, sometimes the field must be visible on the screen to properly dismiss the responder.
If there are no first responders active, keyboard should not appear when alert is dismissed.
For the particular case in this question, I would recommend setting a delegate method to listen for the "done" button and resigning the first responder in the delegate callback.
Alternatively, when beginning editing, you can store a reference to the currently active text field, then in your "clickedButtonAtIndex" method you can resign the active text field if it is still active.
I've noticed some weird behavior with textField keyboards and alertViews as well... Maybe make a bool called disableKeyboard and use it like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (disableKeyBoard) {
disableKeyboard = NO;
return NO;
} else {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
disableKeyboard = YES;
}
This is just a workaround and doesn't address the core issue, whatever it is. For this method to work you need to set the alertView and textField delegate methods in your header.
It looks like iOS 8 has a bug where alerts with text input do not show the keyboard. I have tried this hack.
The problem with the hack is that first the alert appears, and only afterwards does the keyboard appear. This causes the alert to "jump up" to make space for the keyboard.
How can I have a UIAlertView with text input, where the keyboard appears immediately?
(Note: For an example of what I want, go to Voice Memos, record a new memo, save, and you'll be prompted to enter a name with a UIAlertView with text input. There, the keyboard appears at the same time as the UIAlertView.)
I am not sure this will perfectly solve the problem of keyboard and alertView appearing simultaneously. But I would recommend you to use the newer api. I am posting this as an answer as its difficult to put code into comments.
For some reasons UIAlertView has been deprecated in iOS 8. Instead of using the UIAlertView you should use the UIAlertController with style UIAlertControllerStyleAlert. Present it and then open keyboard.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:yourTitle message:yourMessage preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
Since the alert now is shown as UIViewController the keyboard won't shift the alert box upside.
//ctrl+k to appear keyboard and ios8 keyboard appearing simultaneously issue would solve by this...
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"" message:#"Registration Successfully" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// [self.view endEditing:YES];
// [self.navigationController popToRootViewControllerAnimated:YES];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
I am trying to understand a weird behavior of my app, here is a description (tested in a trivial project).
ViewControllerA is presenting modally ViewControllerB
ViewControllerB contains a button, this button is presenting a UIAlertController specified this way
alert = [UIAlertController alertControllerWithTitle:#"Test" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:#"Action" style:UIAlertActionStyleDefault handler:^(UIAlertAction *handler) { NSLog(#"Action"); }]];
The ViewControllerB is presenting alert this way
- (IBAction)button:(id)sender {
alert.popoverPresentationController.sourceView = self.button;
alert.popoverPresentationController.sourceRect = self.button.bounds;
[self presentViewController:alert animated:YES completion:nil];
}
Now, if you click on the button, the alert appears, if you click outside the alert, the alert disappears (I am on iPad). You can do it as many times as you want...
Here is the bug: When the alert is presented, if you click twice outside (quickly enough, ~0,2s interval), the alert disappears AND ViewControllerB is dismissed. At the end we can see ViewControllerA but we never asked for it.
There is also a warning message:
Warning: Attempt to dismiss from view controller <UIViewController: 0x7f85ab633f70> while a presentation or dismiss is in progress!
Thank you for your help.
I would prefer to add a UITapGestureRecognizer at the end, on your UIAlertController. like :
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Test"
message:#"Test Message."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *closeAction = [UIAlertAction actionWithTitle:#"Close"
style:UIAlertActionStyleDefault
handler:nil];
UIAlertAction *someAction = [UIAlertAction actionWithTitle:#"Action"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
....
}];
[alertController addAction:closeAction];
[alertController addAction:someAction];
[self presentViewController:alertController animated:YES completion:^{
[alertController.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action:nil]];
}];
I have a problem since iOS8 and Xcode 6.0.1, the title of my alert dialogs doesn't appear. So I changed my UIAlertView to UIAlertController but I have the same issue...
The title of my alert is always to nil and the displaying is very ugly because I don't have the serparator lines.
Do you know why i have this ?
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"title" message:#"message" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alert animated:YES completion:nil];
But this is the result
(lldb) po alert.title
nil
(lldb) po alert.message
message
Thank you.
I resolved my problem thanks to #Myaaoonn !
UIAlertView title does not display
I have just Remove the following method from my ViewController
Category Class And its working fyn!
- (void)setTitle:(NSString *)title
{
// My Code
}
Try calling the UIAlertView like this:
[[[UIAlertView alloc] initWithTitle:<#title#>
message:<#msg#>
delegate:nil
cancelButtonTitle:<#cancel button#>
otherButtonTitles:<#buttons#>,nil] show];
Does the title still not show?