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.
Related
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;
}
}
When I press the 'OK' button at the UIAlertView I wanted it to go back to UITableViewController but when I click it doesn't go back.
QuizViewController.h:
#interface QuizViewController : UIViewController <UIAlertViewDelegate> {
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
QuizViewController.m
-(IBAction)buttonWasClicked:(id)sender{
UIButton *resultebutton= (UIButton*)sender;
if (qCount < totalQuestions) {
id prevQuestion = [appDelegate.qs objectAtIndex:qCount-1];
NSString * correctAns = [prevQuestion labelAns];
if ([correctAns isEqualToString:resultebutton.titleLabel.text])
myScore += 5;
NSLog(#"The button title is %# ", correctAns);
NSLog(#"The button title is %# ", resultebutton.titleLabel.text);
NSString *finishingStatement = [[NSString alloc] initWithFormat:#"Your score so far is %i!", myScore];
theScore.text = finishingStatement;
id nextQuestion = [appDelegate.qs objectAtIndex:qCount];
quizLbl.text = [nextQuestion labelQn];
headerLbl.text = [nextQuestion labelHeader];
[qBtn setTitle:[nextQuestion labelBtn] forState:UIControlStateNormal];
[qBtnB setTitle:[nextQuestion labelBtnB] forState:UIControlStateNormal];
[qBtnC setTitle:[nextQuestion labelBtnC] forState:UIControlStateNormal];
[qBtnD setTitle:[nextQuestion labelBtnD] forState:UIControlStateNormal];
qCount++;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results" message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
QuizTableViewController *quizTable = [self.storyboard instantiateViewControllerWithIdentifier:#"quizTable"];
[self.navigationController presentModalViewController:quizTable animated:YES];
}
You should not declare a delegate method in your .h file like:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
instead give the delegate:self in alertView like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results" message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
and declare in your .h file like
#interface QuizViewController : UIViewController<UIAlertViewDelegate>
and then use [self.navigationController presentModalViewController:quizTable animated:YES];
in your delegate method at the click of button.index=0.
The reason why you don't get called back is that you set your UIAlertView delegate to nil. It needs to be set to self in order for that object to receive a callback when the alert is dismissed:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Results"
message:[[NSString alloc] initWithFormat:#"Your total score will be %i!", myScore]
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles: nil];
Use this:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if([alertView.title isEqualToString:#"the_title_of_your_alert"]){
//above line is to identify your alert, if you have several ones
if(buttonIndex == 0)
//do this
else if (buttonIndex == 1)
//do that
else if //bla bla bla, find the button, and if it is your "ok" button,
//go to your TableViewController
}
}
And alertView's delegates should not be declared that way, just implement the delegate methods.
Use this code to go back.
[self.navigationController popViewControllerAnimated:YES];
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.
Using core data to populate my table view. The thing I am not getting is that how can I delete a single entry from the core data.
I am using uitableview not the controller.
Here is the code I am using:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == favouritesTable) {
cellValue = [licensePlateArray objectAtIndex:indexPath.row];
} else { // handle search results table view
cellValue = [filteredListItems objectAtIndex:indexPath.row];
}
static NSString *CellIdentifier = #"vlCell";
VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"Cell Created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"VehicleListCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[VehicleListCell class]]) {
cell = (VehicleListCell *)currentObject;
}
}
UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(tableCellPressed:)];
pressRecongnizer.view.tag = indexPath.row;
pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer];
[pressRecongnizer release];
}
cell.textLabel.font = [UIFont systemFontOfSize:10];
Favouritesdata *favdata = [licensePlateArray objectAtIndex:indexPath.row];
[[cell ignition] setImage:[UIImage imageNamed:#"ignition.png"]];
[[cell direction] setImage:[UIImage imageNamed:#"south.png"]];
cell.licPlate.text = [favdata licenseplate];
NSLog(#"cellvalue for cellforRow: %#", cell.licPlate.text);
return cell;}
In the method of UILongPressGestureRecognizer:
- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{
if (recognizer.state != UIGestureRecognizerStateBegan) {
return;
}
VehicleListCell* cell = (VehicleListCell *)[recognizer view];
cellValueForLongPress = cell.licPlate.text;
NSLog(#"cell value: %#", cellValueForLongPress);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil] ;
alert.tag = recognizer.view.tag;
[alert addButtonWithTitle:#"Remove from Favourites"];
[alert addButtonWithTitle:#"Take to Map"];
[alert show];}
here in alert view method the selected row will be deleted (if([title isEqualToString:#"Remove from Favourites"])):
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alert buttonTitleAtIndex:buttonIndex];
NSManagedObjectContext *contextFav = [app managedObjectContext];
Favouritesdata * favourites = [NSEntityDescription insertNewObjectForEntityForName:#"Favouritesdata" inManagedObjectContext:contextFav];
if([title isEqualToString:#"Remove from Favourites"])
{
NSLog(#"cellValueForLongPress: %#", cellValueForLongPress);
///////// to remove the object from core data
[licensePlateArray removeObjectAtIndex:alert.tag];
}
else if([title isEqualToString:#"Take to Map"])
{
NSLog(#"Go to MapView");
}
NSError *error;
if (![context save:&error]) {
NSLog(#"Error Occured");
}
[favouritesTable reloadData];}
Write this in cellForRowAtIndexPath
UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(tableCellPressed:)];
pressRecongnizer.view.tag = indexPath.row;
pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer];
[pressRecongnizer release];
and use alertview like this
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil] ;
alert.tag = recognizer.view.tag;
[alert addButtonWithTitle:#"Remove from Favourites"];
[alert addButtonWithTitle:#"Take to Map"];
[alert show];
then write this in -(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
[YourArray removeObjectAtIndex:alert.tag];
[YourTable reloadData];
Please use this code at appropriate place in your code..
Happy Coding..
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.