How to show an AlertView with another AlertView - iphone

I want to show nested alertViews. Problem, which i am facing in nested alertViews is that when i click an "add" button of first alertView it shows the second alertView, in second alertView i have a textField and a "Save" button. I want to save data when i click on save button and then reload UITableViewData, which is already in the first alertView.
I am new in iphone, so please help me.

You should create your alert views with different tag property so that in delegate method you can easily differentiate which alert view is appeared on the screen.
For example :
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Info"
message:#"Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[alert setTag: 1001]; // give different tag to different alert views
[alert show];
[alert release];
Now in delegate method :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1001)
{
// do something
}
eles if (alertView.tag == 1002)
{
}
}
Hope it helps you..

Related

UIAlertViewDelegate not acting as expected

I have a very simple process running where after each round of a simple game the scores are calculated, labels updated and all the normal, very simple stuff. I have a UIAlertView that informs the player of how s/he performed. I use a UIAlertViewDelegate to postpone all the updates, resetting of controls etc. till after the UIAlertView is dismissed. The methods are [startNewRound],[startOver] and [updateLabels]. It's fairly obvious what they all do. Anyway, when the user hits round ten, I've made another UIAlertView that informs the player that the game has ended and shows the overall score. Again, I hoped to use a delegate to postpone the resets till after the AlertView is dismissed. The only problem is, with the endGame AlertView, it seems to be using the first AlertView's delegate method causing the game to continue with a new round and not start from the beginning. I hope this makes sense. Anyway, here are snippets of my code.
if (round == 10){
UIAlertView *endGame = [[UIAlertView alloc]
initWithTitle: #"End of Game"
message: endMessage
delegate:self
cancelButtonTitle:#"New Game"
otherButtonTitles:nil];
[endGame show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate:self
cancelButtonTitle:#"Next"
otherButtonTitles:nil];
[alertView show];
}
And then the delegate methods:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startNewRound];
[self updateLabels];
}
- (void)endGame:(UIAlertView *)endGame didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startOver];
}
So there it is. As I mentioned, the endGame AlertView appears to be using alertView's delegate, thus not activating the [self startOver] method. All the methods are working, it's just the AlertView is using the incorrect delegate method. Regards, Mike
Change your code like this,
if (round == 10){
UIAlertView *endGame = [[UIAlertView alloc]
initWithTitle: #"End of Game"
message: endMessage
delegate:self
cancelButtonTitle:#"New Game"
otherButtonTitles:nil];
endGame.tag = 111;
[endGame show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate:self
cancelButtonTitle:#"Next"
otherButtonTitles:nil];
alertView.tag = 222;
[alertView show];
}
and delegate method as,
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 111)
{
[self startNewRound];
[self updateLabels];
}
else if(alertView.tag == 222)
{
[self startOver];
}
}
You cant have two delegate method for dismisswithbuttonindex, you need to handle this situation with tag.
Give both alert view a different tag and check it on delegate object. Thus you can differentiat the both alert view.

Don't Pop Navigation Controller in Xcode if AlertView

I have a Navigation Controller and when I press the back button I don't want to pop the view but I want to show an UIAlertView.I just want to pop the view after having make a choice on the UIAlertView!
How can I do?
I tried to catch the event 'back button pressed' but with no results :(
Create a UIBarButtonItem inside viewDidload and added it to the navigation bar
UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;
create a method which will be called when the backButton is clicked
-(void)backButtonClicked{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Pop View!" message:#"Are you sure?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
write this UIAlertView delegate method which will be called when the button in the UIAlertView is clicked
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.navigationController popViewControllerAnimated:YES];
}
}
NOTE: Dont forget to add UIAlertView delegate in the header file <UIAlertViewDelegate>
You can check for back-button press in the viewWillDisappear method like this (got it from here):
-(void)viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound && self.isBackButtonPressed) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
Hope this helps!
Implement pop in UIAlertview delegate method clickedButtonAtIndex:
The back button press need to implement only showing the UIAlertview
refer this
follow this code
in button action.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Do you want to open?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alert show];
//Alert View delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//NO
{
//your code
}
else if(buttonIndex==1)//YES
{
// your code
}
}

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.

Multiple alertviews in a single view?

I have an Iphone application in which when i am pressing a button it shows an alertview to chose the background.whichever background user is chosing will be played as the background of the audio clips.But now i need to add another alert before i am showing this alert for giving some warning.after that only i need to pop the second one.but i was done that chosing alert in the didappear of that viewcontroller and set it as a Uialertview delegate.and on the button actions i was doing different actions.Can anybody help me on achieving this?
proAlertView *loginav1=[[proAlertView alloc] initWithTitle:#"title" message:#"Choose a Background to play with this program?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Field",#"Beach", #"Stars",nil];
[loginav1 setBackgroundColor:[UIColor colorWithRed:0.129 green:0.129 blue:0.129 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.625 saturation:0.0 brightness:0.8 alpha:0.8]];
[loginav1 show];
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//[self play];
//moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
if(actionSheet.tag==123)
{
[self backButtonPressed];
}
}
else if (buttonIndex == 1)
{
videoFile = [[NSBundle mainBundle] pathForResource:#"video-track" ofType:#"mp4"];
[self play];
moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
}
how can i include another alert before this is my question?
Initialize first Alertview
UIAlertView *al1 = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Warning Msg!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
al1.tag=1;
al1.delegate=self;
[al1 show];
Implement Delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==1){
// implement button events for first Alertview
if(buttonIndex==1){
//First button clicked of first Alertview
UIAlertView *al2 = [[UIAlertView alloc] initWithTitle:#"Choose BG" message:#"Choose BG?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"1",#"2",#"3", nil];
al2.tag=2;
al2.delegate=self;
[al2 show];
}
}
if(alertView.tag==2){
// implement button events for second Alertview
if(buttonIndex==1){
// First button clicked second Alertview.
}
}
}
Controller Class header
#interface ViewController : UIViewController<UIAlertViewDelegate>{
}
Hope this will fulfill your need !
You can do like this, first display warning message in alertview and when user click OK in alertview then in alertview delegate method write code to display second alertview where user can choose background.

about UIAlert jumps to one xib

I have some three xib files, a b c
there is a button on b,and it will show an alert when click it,the code is
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Warning"
message:#"You will jump to a when you click 'Yes,Go',or click 'No,I am not' for exit"
delegate:nil
cancelButtonTitle:#"Yes,Go"
cancelButtonTitle:#"No,I am not"
otherButtonTitles:nil];
[alert show];
[alert release];
as its description, I want to jump to the a when I click the 'Yes,Go',this app will be closed if I click 'No,I am not'
So what should do?
Thanks
you can handle the event in alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
here check for button index and do what you want.
First of write your UIAlertView like below,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"You will jump to a when you click 'Yes,Go',or click 'No,I am not' for exit"
delegate:self
cancelButtonTitle:#"Yes,Go"
otherButtonTitles:#"No,I am not"
,nil];
[alert show];
[alert release];
then you can handle which button is tapped by user as below code:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==0)
{
//handle it for "Yes,Go"
}
else if (buttonIndex ==1)
{
//handle it for "No,I am not"
}
}
We are missing a lot of details here to answer this question I think. What is the structure of your program? How do you show the xib files etc.