about UIAlert jumps to one xib - iphone

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.

Related

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.

How to exit the program after detecting user clicked the OK button on the UIAlertView

In my program, I have a code as below. How to exit the program after DETECTING user click OK on the UIAlertView?
Thanks
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"YOur Message" message:#"Your description"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
For getting the cancel (your "OK") button Implement this method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
exit(0);
}
Check the QA here.
see this tutorial
if You want then use exit(0);
set AlertView delegate to self. and do your task in following delegate-
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
For capturing the ok pressed, use this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
exit(0);
}

Alert message for the button click in iphone sdk 4.3

I am beginner to xcode programming.Please tell me how to display the alert message when we are going to click the button in xcode-iphone-4.3
My Code is as follows,
- (IBAction)buttonPressed:(id)sender{
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:#"Hello World!!!!!!"
message:#"This is the Iphone app" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[mes show];
[mes release];
Please help me regarding this.
-(IBAction)buttonOnePressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Clicked button 1"
message: #"Alert Message here"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert setTag:1];
[alert show];
}
-(IBAction)buttonTwoPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Clicked button 2"
message: #"Alert Message here"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert setTag:2];
[alert show];
}
Below is the delegate method to track which button on Alertview is hit.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) { // UIAlertView with tag 1 detected
if (buttonIndex == 0)
{
NSLog(#"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
else if (alertView.tag == 2) { // UIAlertView with tag 2 detected
if (buttonIndex == 0)
{
NSLog(#"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(#"user pressed Button Indexed 1");
// Any action can be performed here
}
}
}
You can set tag to UIAlertView in case you have more than one UIAlertViews and can determine which UIAlertView button is clicked in its delegate method clickedButtonAtIndex using its respective tag.
In IBAction you have to write the code and give the Connections to the Button
Create the IBAction for your button and add the code for alert view in that method.

How to show an AlertView with another AlertView

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..

Why does this code not show an alert?

The following code should show a UIAlertView in response to a button being clicked in a UIActionSheet, but I can't figure out why it doesn't work. Does someone have an idea as to why this is?
-(void)actionsheet:(UIActionSheet *)actionsheet willDissmissAtButtonIndex:(NSInteger)
buttonindex{
if(buttonindex =[actionsheet cancelButtonIndex]){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"welcome"
message:#"thanku for watching" delegate:self cancelButtonTitle:#"bye"
otherButtonTitles:nil];
[alert show];
[alert release];
}
there is an = missing :)
EDIT
check this
if(buttonindex == [actionsheet cancelButtonIndex]){