UITableView and the alertView: clickedButtonAtIndex method - iphone

I am playing around with xCode and was trying to create a small app that comprises of a table view (built using an array) comprising the names of various individuals. The user would be able to click on one of the rows in the table view and would be shown a UIAlert with an option to call the person or cancel.
If they click on cancel, the UIalert dismisses. If they click on the Call button, I want to launch the Phone application and dial their number.
Here's what I have in terms of code thus far:
//Generate the UIAlert when the user clicks on a row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexpath
{
NSUInteger row = [indexpath row];
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:#"Contact %#", rowValue];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Call this Office"
message:message
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Call",nil];
[alert show];
[message release];
[alert release];
}
//Detect which of the UIAlert buttons was clicked and dial a different number
//based on the index of the original row that was selected
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (row)
{
case 1:
if (buttonIndex != [alert cancelButtonIndex])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://123456789"]];
}
break;
case 2:
if (buttonIndex != [alert cancelButtonIndex])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://987654321"]];
}
break;
default:
break;
}
}
Here's my question - I know that my switch statement above won't work - That method has no idea what "row" is. So, how exactly do I pass in/access the index of the row that the user tapped for use with my switch statement?

When you create the UIAlertView right before you say [alert show] set its tag
alert.tag = [indexPath row];
now in your switch(row) statement do
switch(alert.tag)

Related

UITableView Edit Mode Add the cell defined by user?

I wanted to use the table editing mode so that the users can choose which Cells to add.
For eg I have
addArray = {#"Red",#"Blue",#"Green"};
Now , what I want is that when user enters the editing mode and presses the add button, he gets the options: Red,Blue,Green and selects the cell to be added.
-(IBAction)addButtonPressed
{
[displayArray addObject:"User's Choice"];
[self.tableview reloadData];
}
You probably want to use a UIActionSheet (see Apple docs):
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Select an option"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Red", #"Green", #"Blue", nil];
[actionSheet showInView:self.view];
You will need to make your view controller conform to the UIActionSheetDelegate protocol:
#interface MyViewController : UITableViewController <UIActionSheetDelegate>
Then, implement the actionSheet:clickedButtonAtIndex: method in your view controller:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.firstOtherButtonIndex) {
// Red
[displayArray addObject:"Red"];
[self.tableview reloadData];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1) {
// Green
[displayArray addObject:"Green"];
[self.tableview reloadData];
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 2) {
// Blue
[displayArray addObject:"Blue"];
[self.tableview reloadData];
}
}

Adding a value to the predefined array for uitableview iphone

How can i add a new value to the predefined array for uitableview while clicking add button?
-(IBAction)addNewBreed:(id)sender
{
UIAlertView *addAlert=[[UIAlertView alloc]initWithTitle:#"Add New Item" message:#"\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add",nil];
UITextField *addTextField=[[UITextField alloc]initWithFrame:CGRectMake(12,45,260,25)];
[addTextField becomeFirstResponder];
[addTextField setBackgroundColor:[UIColor whiteColor]];
addTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
[addAlert addSubview:addTextField];
[addAlert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Add"])
{
//HERE WHAT CODE SHOULD I ADD,MY ARRAY NAME 'ar'
}
else
return;
}
If your array is a NSMutableArray, then you can use following code:
[ar addObject: addTextField.text];//you may also enter any string here
[yourTableView reloadData];

UIAlertViewStylePlainTextInput - Program continues without waiting for text input from user

I'm sort of stuck on a UIAlertViewStylePlainTextInput. (My code is too long to post here, but this is the part where the problem exists.) It all sort of works, but the program doesn't wait until the user enters the information before continuing. The "editName" button doesn't change the display in the code below, but the updateDisplay button works fine (if you've pressed the "editName" button before you pressed "updateDisplay"). How can I get everything to sort of halt until the user enters the info (or cancels). Or is there some other lead that will take me where I need to go. (I've been toying with putting the rest of the code in the -(void) alertView, but that just doesn't seem to be the correct way of doing it).
Thanks in advance.
#import "HSTestViewController.h"
#implementation HSTestViewController
#synthesize display;
NSString *newName;
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
newName = [[alertView textFieldAtIndex:0] text];
}
}
- (void) getNewName;
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"You made the High Score List"
message:#"Please enter your name"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
- (IBAction)editName:(id)sender
{
[self getNewName];
newName = display.text;
}
- (IBAction)updateDisplay:(id)sender
{
display.text = newName;
}
#end
Add UIAlertViewDelegate to the class. And implement the below delegate method.
OK button index value will be 1 in your case.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
{
// call the method or the stuff which you need to perform on click of "OK" button.
}
}
Hope this helps.

Show Alert in clickedButtonAtIndex?

i need to show a confirm alert after the user press buttonIndex 1 but... if i use popViewcontroller in clickedButtonAtIndex it crash without errors.
The problem is that
[self.navigationController popViewControllerAnimated:YES];
is called before second Alert click...
how to fix?
This is my code:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"OK!"
message:#"Completed"
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
}
}
Set the tag properties of the two UIAlertViews to 1 and 2, respectively. Then, in the delegate method, use if statements to check the tag of the UIAlertView argument.
Example:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
//check the button index
//create and display the other alert view (set the tag property here to 2)
}
else if (alertView.tag == 2)
{
//pop the view controller
}
}

UIAlertView button tagging

Is there a way I can add a .tag to an UIAlertView button? Reason being, I'm adding a few dynamic buttons to the alert that will sometimes be in the alert and sometimes not. I figured the best way was to add a tag. Is there a better method for this?
The options that will ALWAYS be in the alert are Email, Save. And the 2 optional options are Tweet This and Facebook.
Thanks for any help in advance!
There is one method buttonTitleAtIndex for UIAlertView. Use that to find the button tapped by user.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if( [buttonString isEqualToString:#"Facebook"] ){
// your code here
} else if( [buttonString isEqualToString:#"twitter"] ){
// your code here
}
}
You can also use tag proprerty:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Info"
message:#"Info text"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert setTag:0];
Then in delegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (alertView.tag) {
case 1: {
...
break;
}
...
default:
break;
}