UIAlertView showing after refresh view - ios

I Have this code:
else if ([response isEqualToString: #"1"])
{
UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:#"FAILED" message:#"Please, Try Again" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView1 show];
}
I catch from a JSON response an error and show this alert.
This code is into a post method of a ViewController with table view, and this view controller is a parent to another view controller. The problem is that AlertView not showing (or showing fast) in the present view (the title in navigation bar become dark), but if I going back to the parent view controller and return to this view (practically, if i "refresh" the view), the alert show correctly.
I have the same code for iPad and iPhone, but in iPad not done.

Related

how to modify the text of the alert view which is shown when i have finished jumped to other application

Now I saw a alert view as shown below.
How to modify the text which tells me to choose the action.
the text which is end with X is what I want to modify.
thanks.
That does not look like an alert box. Alerts are usually called as
alert(text);
where text is what that will be displayed in the popup followed by an OK.
Create an alert view and add title, message and text of buttons to be shown. For example:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Other", nil];
[alertView show];

Getting around shouldPerformSegueWithIdentifier's paradox

I'm an iOS newbie trying to get this right...
I'm trying to use shouldPerformSegueWithIdentifier in order to add some validation before allowing the segue to go through.
I'm using an Exit segue through a Bar Button, whose method (unwindToChecklist) is written in the destination view controller (called ChecklistViewController). I'm assuming that's why it isn't working, because the source view controller (AddItemViewController) has no segue called unwindToList, even though it's listed under AddItemViewController's list of elements on the left sidebar in the Storyboard.
I've already ensured that the segue's identifier is named unwindToChecklist in the attributes inspector.
My code:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
NSLog(#"Checking...");
if ([identifier isEqualToString:#"unwindToChecklist"]) {
if (![self.txtItemTitle.text isEqualToString: #""]) {
return NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Name Empty"
message:#"Please fill in the title name text field."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
} else {
return YES;
}
}
return YES;
}
Screenshots of 1) the scene sidebar & 2) attributes inspector:
1)
2)
Thanks in advance!

ios 7 showing alert view in viewDidAppear causes view to not appear

I'm pushing a view on to my navigation controller -
[self.navigationController pushViewController:_gameOverViewController animated:YES];
In the new view's viewDidAppear, I show an alert view -
UIAlertView* alert = [[[UIAlertView alloc]
initWithTitle:#"alert title"
message:#"some text"
delegate:self
cancelButtonTitle:#"Rate It!"
otherButtonTitles:#"No Thanks",
#"Don't ask again", nil] autorelease];
[alert show];
In iOS 6 and earlier this works fine. The 'gameOver' view is visible behind the alert view. Once the alert view is dismissed the game over view is visible. In iOS 7, the alert view shows over the previous view. When the alert view is dismissed, the previous view is still visible. The 'gameOver' view is never presented to the user.
Is there a better way to show UIAlertView in iOS 7?
It came down to popping a view off the navigationcontroller and pushing a new one on at the same time. in ios6 and below this worked fine. in ios7 it appears that you cant push a new view onto the stack until the previous animation has stopped.

Asking for user confirmation before leaving a view in iOS

I need to show an UIAlertView before a user leaves a certain view, either by tapping a 'back' navigation bar button or by tapping one of the tab items in the tab bar I have, in order to ask him for confirmation. It would be a two-button alert, a 'Cancel' one to stay in the view, and an 'Accept' one to leave. I need to do this because I have to make the user aware that unsaved changes will be lost if leaving.
I tried to do this by creating and showing the alert view in the viewWillDisappear: method:
- (void)viewWillDisappear:(BOOL)animated
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Exit", #"")
message:NSLocalizedString(#"Are you sure you want to leave? Changes will be discarded", #"")
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel", #"")
otherButtonTitles:NSLocalizedString(#"Accept", #""), nil];
[alertView show];
[super viewWillDisappear:animated];
}
But the view is pop anyway, and the alert view is shown after that and app crashes since its delegate is the view controller that has been already pop from the navigation stack... I don't find the way to solve this scenario, can anybody help me?
Thanks!
Showing the alert view when viewWillDissapear won't work, because the view is already dissapearing, it's on its way to be removed.
What you can do, is add yourself a custom action when the back button is pressed, then you decide what to do when the back button is pressed, you can show the alert view, and then in one of the buttons procedd to dismiss the view, something like this:
- (id)init {
if (self = [super init]) {
self.navigationItem.backBarButtonItem.target = self;
self.navigationItem.backBarButtonItem.action = #selector(backButtonPressed:);
}
return self;
}
Then show the alert view when the back button is pressed:
-(void)backButtonPressed:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Exit", #"") message:NSLocalizedString(#"Are you sure you want to leave? Changes will be discarded", #"") delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", #"") otherButtonTitles:NSLocalizedString(#"Accept", #""), nil];
[alertView show];
}
Now, when the confirmation button in the alert view is pressed, just call:
[self.navigationController popViewControllerAnimated:YES];
Or do nothing if the user cancels
I would be tempted to move the data manipulation you're trying to protect into a modal view controller and handle the validation on whatever action you choose to have dismiss the modal presentation. To me, that's the point of modal: something that has to be completed before interacting with the rest of the app.

app crashes when clicks on alert in ios

Following are the code for displaying alert in a view controller
-(void)saveProducts {
pData = [[JsonModel sharedJsonModel] prodData];
if ([pData count] == 0 && [self respondsToSelector:#selector(alertView:clickedButtonAtIndex:) ] ) {
alert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"No products against this category" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[self.tblView reloadData];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
[actInd stopAnimating];
}
}
But in slow network, alert will come slowly. If we click on back button of navigation bar at the mean time, pop the navigation controller and displaying alert in new view controller. But when i clicks on OK, app suddenly crashes with EXC_BAD_ACCESS error.
I also tried
didDismissWithButtonIndex
function instead of
clickedButtonAtIndex
But same error occurs. Please help me
It works normally if we didn't click on back bar button. Problem only arises when first view controllers alert displays in second view controller
EDIT
This is the error report
* -[ProductsListing alertView:didDismissWithButtonIndex:]: message sent to deallocated instance 0x8478280
EDIT
I understand the problem. When I click on back button, my alert delegate deallocates and delegate calling results error. How can I overcome this?
My best guess is that either 'self.navigationController' or 'actInd' have already been released. Also, your 'UIAlertView' leaks memory (unless you're using ARC). Profile the app using Instruments, selecting the "Zombies" tool and see what it comes up with.
I believe you have to change
[alert show];
to
if(self.view.window){
[alert show];
}
This way the alert appears only if the controller(the view) is still on screen.(why let the user see an alert from a previous screen?)
If you want the alert to appear anyway....then the "old" controller must inform the "new" one that a problem occurred...and now its the new controller's job to inform the user.
Or you can try changin this part
[self.navigationController popViewControllerAnimated:YES];
[actInd stopAnimating];
to
if(self.view.window){
[self.navigationController popViewControllerAnimated:YES];
[actInd stopAnimating]; // im not sure where the animation is...so not sure if this shoulb be in here or not
}
From what you described the problem here may be this(a wild guess)
[actInd stopAnimating];
called after the viewController is removed(popped).the actInd may not have a valid memory and hence it crashes
change the method content like this and check
if (buttonIndex == 0) {
[actInd stopAnimating];
[self.navigationController popViewControllerAnimated:YES];
}

Resources