UIAlert View-for yes/no condition - iphone

my app needs alert msg and if yes button pressed then one more alert msg and then i have to called a method.This is my code:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag =2;
[alert show];
[alert release];
}
else if(alertView.tag ==2)
{
[self resetArray];
}
}
Thanks.

I'm not sure what your goal is but a few things look wrong to me anyways:
First of all you should create your strings this way:
NSString *title= #"Warning";
There's no need to use stringWithFormat in your case.
Then, it doesn't seem you properly set the first UIAlert's tag to 1, and the default value for tags is 0 so I guess the if statements in didDismissWithButtonIndex are never true.
Also, you should check which button was pressed using buttonIndex, otherwise you are going to show both alert and call [self resetArray] whichever button is pressed by the user.
Hope that helps.

In your code, you create the first alert, but never actually set the tag on it. You should do:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];
Then the code in your delegate method will run.

Please define two separate UIAlertView in .h file
#interface XYZViewController:UIViewController
{
UIAlertView *firstAlertView;
UIAlertView *secondAlertView;
}
Now in your .m file modify as below:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(firstAlertView == nil)
{
firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[firstAlertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == firstAlertView)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(secondAlertView == nil)
{
secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[secondAlertView show];
}
else if(alertView == secondAlertView)
{
[self resetArray];
}
}
and in dealloc method please release the allocated UIAlertviews.
Hope i am clear to you.
Thanks,
Jim.

Related

Unable to update uitableview rows

I am trying to update selected rows of UITableView from UIAlertView inside didSelectRowAtIndexPath. But I am facing a problem. Each time I try to update any row, it's only updates the first row irrespective of selection of row.
My code is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedrow = nil;
if (searching) {
slCell=indexPath.row;
selectedrow = [copyListOfItems objectAtIndex:indexPath.row];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter the Number of Quantity" message:#""
delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#"" label:#"Number of Item"];
textfieldQty = [alert textFieldAtIndex:0];
textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter the Number of Quantity" message:#""
delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert addTextFieldWithValue:#"" label:#"Number of Item"];
textfieldQty = [alert textFieldAtIndex:0];
textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *b=nil;
if (buttonIndex == 0) {
if (searching) {
if([textfieldQty.text isEqualToString:b]) {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Enter plz" message:#""
delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK"];
[alert2 show];
[alert2 release];
}
NSString *newqty = [[NSString alloc] initWithFormat:#"%#",textfieldQty.text];
DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
[d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];
NSLog(#"tb%#",copyListOfQty);
}
else {
NSString *newqty = [[NSString alloc] initWithFormat:#"%#",textfieldQty.text];
DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
[d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];
[self.tablevieww reloadData];
}
}
}
Please follow below steps to achieve what you want.
While creating UIAlertview in (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, set alerview's tag as indexpath's row.
In delegate method -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, you try to access alertView's tag and pass that tag to the method call [d->tableDataQt replaceObjectAtIndex:[alertView tag] withObject:(id)newqty];
In AppDelegate class, you create one more method called -(void)replaceObjectAtIndex:(NSInteger>inIndex withObject:(id)inObject
In above newly created method try to access UITableViewCell from UITableView object with method call UITableViewCell *myCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:inIndex inSection:0];
This way, you will get the object of UITableViewCell and alter the value of that and reload the table.
I hope this will help you. Please do not forget to put right mark if this resolves your problem. :)
Thanks.

How to show a UIAlertView upon selection of an item in UIPickerView

In my picker view i have a "Custom" option which should popup a UIAlertView for the user to enter a new value, the value is saved in the plist source of the picker for future reference. xxxEditingDidBegin is being called repeatedly (never ending).
I presume its because my UIAlertView is triggering the picker to close.
How should I have done this?
- (IBAction)serviceTypeFieldEditingDidEnd:(UITextField *)sender
{
UIPickerView *picker = [sender.inputView.subviews objectAtIndex:0];
NSString *selText = [serviceTypeArray objectAtIndex: [picker selectedRowInComponent:0]];
sender.text = selText;
if (NSOrderedSame==[selText compare:#"Custom"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
}
Implement method like
- (IBAction)doSelectDate:(UIDatePicker *)sender
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Hi" message:#"AlertView is shwoing" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
and connect above method with ValueChanged Event of UIPickerView;
I fixed it like this
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *rowString = [serviceTypeArray objectAtIndex:row];
if ([rowString compare:#"Custom"] == NSOrderedSame)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
else
{
_serviceType.text = rowString;
}
}

Alert with instructions before gameplay starts

How can I add an alert that displays instructions before game starts:
See code below:
- (void)viewDidLoad
{
[super viewDidLoad];
if (questions && configDictionary) {
[questionLabel setText:[[questions objectAtIndex:currentQuestonIndex] objectForKey:#"question"]];
NSArray *answers = [[questions objectAtIndex:currentQuestonIndex] objectForKey:#"answers"];
[answerLabel0 setText:[answers objectAtIndex:0]];
[answerLabel1 setText:[answers objectAtIndex:1]];
[answerLabel2 setText:[answers objectAtIndex:2]];
[answerLabel3 setText:[answers objectAtIndex:3]];
[pointsPerAnswerLabel setText:[NSString stringWithFormat:#"+%d points", [[configDictionary objectForKey:kPointsPerCorrectAnswer] intValue]]];
[currentQuestionNumberLabel setText:[NSString stringWithFormat:#"question %d", currentQuestonIndex+1]];
}
}
Use a UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Instructions"
message:#"Your Instructions..." delegate:self cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
If you want to alert the user every time the app launches place it in the
- (void)applicationDidFinishLaunching:(UIApplication *)application {
}
Edit
You said you wanted to start the game after the dismiss button is pressed. So take advantage of the UIAlertView delegate:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0){
//Start your game!
}
}
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"How to play"
message:#"Answer the questions correctly to get points blablabla..."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];

How to dismiss alertview when click on the button in that alertview

I am new to iOS.
I am working on alertviews. Here is my code. Here there are 2 alertviews: successfulallert and unsuccessfulallert for login page. I am using alertview delegate also here, it will work for both alertviews but I want to work only for successful alertview and navigation should be done only for successful alertview. If anybody knows this please help me.
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
}
Why don't you put "OK" as cancelButtonTitle? Everything will be handled automatically.
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[successfulAllert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//POP here with this:
[self.navigationController pushViewController:addItemView animated:NO];
}
}
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 2)
{
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
else
{
//[[self navigationController]pushViewController:registerUserScreen animated:NO];
// OR
return;
}
}
You have many ways to correct your code, the first and very common is to use the tag property (integer) of the UIView. Since UIAlertview inherits from UIView, it has the tag property, so each time you want create an alert (or a view), set the tag and the check your condition like:
...
alert.tag=1;
[alert show];
then to know wich alert is calling the callback:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==theTagOfYourAlert){
//do your stuff
}
}
another way, in your case, could be:
if([alertView.title isEqualToString:#"Alert"]){
//do your stuff
}
}
Add tag to the two alert views and check for tag in alert view delegate.
Sample code:
NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
// NSLog(#"string= %#", str);
match = [responseOfResult rangeOfString: #"successful"];
if(match.location == NSNotFound)
{
UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:responseOfResult
delegate:self
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[unsuccessfulAllert setTag:1];
[unsuccessfulAllert show];
}
else {
UIAlertView *successfulAllert = [[UIAlertView alloc]
initWithTitle:#"Message" message:#"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[successfulAllert setTag:2];
[successfulAllert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==2 && buttonIndex == 0){
[[self navigationController]pushViewController:registerUserScreen animated:YES];
}
Yes the delegate would work for both the alertviews but you can assign a tag to each alertview Object and check for the tag in the delegate and then perform event if the tag for that particular AlertView onject matches.If u need code , i will provide.
For things like Login status updates, you might want to have the "Login Successful" message disappear automatically. Try this instead:
https://github.com/camclendenin/flashbox
This works nicely and comes in handy for situations like this. Plus you don't have to deal with all the clutter involved with UIAlertViews.

UIAlertView show multiple message

how can i show multiple messages in my alertView from different variables?
Build the desired NSString from your multiple variables, e.g.:
NSString *foo;
NSString *bar;
NSString *baz;
// ... set values for foo, bar and baz ...
NSString *myMessage = [NSString stringWithFormat:#"%# %# %#", foo, bar, baz];
Then set your alert view to use the composite message myMessage:
NSString *myTitle = #"xyz";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: myTitle
message: myMessage
delegate: nil
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
NSString *string1=#"total time played:30\n";
NSString *string2=#"total score :90\n";
NSString *string3=#"19/2/20010 12:00:77\n";
NSString *string=[NSString stringWithFormat:#"%#%#%#",string1,string2,string3];
UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle:#"Hello" message:string delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[progressAlert show];
[progressAlert release];
NSString *str1=#"Message 1.";
NSString *str2= #"Message 2.";
NSString *str3 = #"Message 3";
NSString *msg=[NSString stringWithFormat:#"%#\n%#\n%#",str1,str2,str3];
FreeCoinsCustomAlert* alert = [[FreeCoinsCustomAlert alloc] initWithTitle:msg
message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
You may wish to flesh out your question a bit more, but working from my understanding of your question, you should just be able to use the
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
UIAlertView constructor. Then just concatenate your variables into a single NSString object and pass it as the (NSString*)message. The
+ (id)stringWithFormat:(NSString *)format, ...
factory method makes this trivial. I would recommend reading your options with NSString in the Apple documentation.