adding ok button at run time in UIAlertview ios - iphone

I call alert with progress indicator view while calling web services.i am having an alert view set up like this:
[self.activityIndicatorView setHidden:NO];
self.alertView = [[UIAlertView alloc] initWithTitle:#"Sending Login Request.."
message:#"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicatorView.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[self.alertView addSubview:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
[self.alertView show];
Later if login fails I want to put "OK" button on alert view, withot dismissing self.alertView, and again showing new instance of self.alertView.Some thing like this:
if (isThereErrorFromJsonResp) {
[self.activityIndicatorView stopAnimating];
[self.activityIndicatorView removeFromSuperview];
self.activityIndicatorView = nil;
[self.alertView setTitle:isThereErrorFromJsonResp];
//here i want to show ok button how?
return;
}
So how should i put OK button?
Any Suggestion?

Remove the alert on getting the response and display an new instance of alert like this
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
self.alertView = [[UIAlertView alloc] initWithTitle:isThereErrorFromJsonResp
message:#"\n"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[self.alertview show];
SOLUTION
Ok tried out and got it
Use
[alertView dismissWithClickedButtonIndex:0 animated:YES];
[alertView addButtonWithTitle:#"Ok"];
[alertView show];
This will add the button to the alertview

Look at using ATMHud instead - this is a heads-up-display that you can modify while its showing, and can show, start, stop, spinners, add messages etc. When I used it I had a message say "Tap to Cancel", then when the login succeeded, showed a "Success!" for a second or so, then made it disappear. This looks HUD looks very professional in the way it animates, and you have a lot of control over it too.

Try this code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Logout in offline mode may cause of data lose. Do you still want to logout?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES",nil];
alert.tag=11;
[alert show];
[self performSelector:#selector(go:) withObject:alert afterDelay:1.0];
-(void)go:(UIAlertView*)alert
{
UIButton *b = (UIButton*)[alert viewWithTag:1];
b.titleLabel.text = #"test";
}
you will have to add "OK" button initially. And set its property as Hidden = TRUE. and in go method set it property Hidden = FALSE

Related

dismissing a UIAlertView programmatically

I need help on dismissing a UIAlertView programmatically. Currently I have this
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"title" message:#"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
then later on I call this
[alert1 dismissWithClickedButtonIndex:0 animated:NO];
but nothing happens.
You need to set two things.
1. include your .h file : <UIAlertViewDelegate>
2. please follow below implementation...
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"title" message:#"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alert1 show];
[self performSelector:#selector(dismiss:) withObject:alert1 afterDelay:1.0];
the dismiss method will be...
-(void)dismiss:(UIAlertView*)alert
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
I hope this will help you.
I encountered this problem too.
In my case, for some reason calling:
[alert dismissWithClickedButtonIndex:0 animated:NO];
didn't work always (yes, even calling it on UI thread and yes, alert != nil), instead simply setting the animated flag to YES it worked:
[alert dismissWithClickedButtonIndex:0 animated:YES];
Maybe it's an Apple bug...
you should display it first:
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert1 show];
then in delegate method
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if(buttonIndex==0){
// do something
}
}
The methods you called is correct.
I guess the alert1 is nil when your call the method dismissWithClickedButtonIndex:animated:
Try to check your variable alert1.
You can use the delegate method -alertView:didDismissWithButtonIndex: instead—it gets called once the alert view’s been removed from the screen, OR better approach is , use a background thread, e.g. with -performSelectorInBackground:withObject:, to handle whatever processing you need to do.

Multiple alertViews create error

Hello and good afternoon, I'm having some issues here, and to be honest, I don't understand
I have to create different alertViews for the same screen with different messages, most of these alerts only have 1 button, but there's this one to delete that needs 2 buttons, the thing is that, since the others have only 1 button, when I created the 2 button screenview and I added the (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method, I have some problems
some code here
- (IBAction)saveInfo{
if (med.text.length ==0) {
UIAlertView *alertViewError = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"ERROR",#"")
message:NSLocalizedString(#"EMPTY1",#"")
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertViewError show];
[alertViewError release];
}
else if(medicamento.text.length >= 41){
[self lenghtError:40:NSLocalizedString(#"TF_MED",#"")];
}
else if (med.text.length ==0 || descripcion.text.length == 0) {
UIAlertView *alertViewError = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"ERROR",#"")
message:NSLocalizedString(#"EMPTY2",#"")
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertViewError show];
[alertViewError release];
}
else if (descripcion.text.length >= 41){
[self lenghtError:40:NSLocalizedString(#"TF_DESCRIPCION",#"")];
}
else{
[self insertDictionary];
UIAlertView *alertViewAcept = [[UIAlertView alloc] initWithTitle:#""
message: NSLocalizedString(#"ACCEPT_MSG",#"")
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertViewAcept show];
[alertViewAcept release];
[self.navigationController popViewControllerAnimated:YES];
}
}
- (IBAction)cancelData{
UIAlertView *alertViewCancel =
[[UIAlertView alloc] initWithTitle: NSLocalizedString(#"BT_DELETE_MED",#"")
message: NSLocalizedString(#"MSG_DELETE_MED",#"")
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: #"Cancel", nil];
[alertViewCancel setTag:999];
[alertViewCancel show];
[alertViewCancel release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 999) {
if(buttonIndex==0){
[self.Bayer_DB_obj deleteRowWithKeyValue:[NSString stringWithFormat:#"%d",IdMed] onKeyName:#"id_ctl_med" onTable:#"ctl_med"];
// code to delete here
[self.navigationController popViewControllerAnimated:YES];
}
}
}
So, in the first part, I created some alerts to indicate the user that he/she is making a mistake, in the second part, I need a confirmation before deletion, but here, I need 2 buttons, then, in the 3rd part, I have the method that is been called, I added a tag to my alert to avoid doing this comparison in all the alerts, the problem is that, when you show alertViewAcept, it takes you to the previous view controller, and after you click the ok button (that actually is the cancelbuttontitle) the app crashes without any "error message"
so I'm not sure what I'm doing wrong, please help
My guess the problem is that you set the delegate for the alertViewAcept, and right after you showed the alert, you pop the viewController and so your delegate will get released, which will then give you an error once a button on the alert view is clicked.
You should do this:
UIAlertView *alertViewAcept = [[UIAlertView alloc] initWithTitle:#""
message: NSLocalizedString(#"ACCEPT_MSG",#"")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
Even better, all your alerts which only have the OK button, do not need a delegate. And in that case you do not even need the tag.

iPhone alert view exclude a view controller

i have a project with many vie controller, in one of these i create and show a view alert. it's possible show alert in every view exclude one?
I need this because if you are in the alarm view controller you don't need to see the alert when alarm ring
i try this but not works!
// ALERT NOTIFICATION
if (!self.timerViewController) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Piccole Ricette" message:#"READY" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];
}
In iOS you can test whether a ViewController 's view is visible by testing the view's window property. If the view is not visible the window property will be nil. So perhaps you can do something like this:
if (!self.timerViewController.view.window) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Piccole Ricette" message:#"READY" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];
}

UIAlertView not showing message text

I am bringing up an UIAlertView that works fine in portrait layout, but when in landscape mode - the message doesn't appear.
It is a standard UIAlertView, with three buttons.
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"one", #"two", nil];
I have tried moving the buttons down (according to the height of the message label) and resizing the alert according to the relocated buttons, but the message still doesn't appear, despite there being plenty of room for display.
Setting the UILabel background to some color for debugging shows that it just isn't displayed..
EDIT:
The UILabel is there - It's just not being displayed.
In the willPresentAlertView method, I can see the UILabel in the NSAlertView's subviews.
It appears to be a bug with the layout code for UIAlertView. After fiddling a bit in the debugger I managed to get this workaround:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"one", #"two", nil];
[alert show];
// for some reason we have alpha 0 for 3 or 4 buttons
[[[alert subviews] objectAtIndex:2] setAlpha:1];
// also, for 3 buttons the height goes to 10 -> proof of concept 'fix'
[[[alert subviews] objectAtIndex:2] setFrame:CGRectMake(12, 45, 260, 24)];
[alert release];
This is just a proof of concept. A real workaroung should iterate ober the subviews and fix only labels that have either height to small or alpha==0
Probably you missed:
[alert show];
You can directly use uialertview and create object of it. Then pass title and message and button and also other button.....And call click button method.
//Example
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Title" message:#"The message."
delegate:self cancelButtonTitle:#"button 1" otherButtonTitles:#"button", nil];
[alert show];
[alert relaese];
//Then use this method
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the ok/cancel buttons
if(buttonIndex==0)
{
NSLog(#"Ok");
}
else
{
NSLog(#"cancel");
}
}

UIAlertViews disappear when calling UIActivityIndicatorView method

if I show an Alert and then call a method of an Activity Indicator, the alert disappears like the user has pressed OK.
Declaration of alert:
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
Declaration of Activity Indicator:
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.hidesWhenStopped = YES;
[indicator stopAnimating];
Method call which causes this problem:
[indicator stopAnimating];
What can I do?
Edit: I noticed that my app isn't working correct, if NSXML Parser is getting a wrong URL. At the moment I cannot say if the activity indicator is causing a error.
you could have the ActivityIndicator embedded in your alertview, looks neat and tidy:
http://kwigbo.com/post/394670170/iphone-sdk-uialertview-with-activity-indicator
Edit: Re-read your question, above solution might not be suitable for your needs.
Let me ask, what exactly are you trying to do with those two views?