UITableView Edit Mode Add the cell defined by user? - iphone

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];
}
}

Related

How to distinguish between UIAlertView Cancel and UIAlertViewStyleSecureTextInput's TextView Return?

I have created a UIAlertView
alert = [[UIAlertView alloc] initWithTitle:#"Test"
message:#"Working"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
alert.tag = kAlertTypePIN;
UITextField *textField = [alert textFieldAtIndex:0];
textField.delegate = self;
If I press Retun key in UIAlertView textfield it works fine, it calls:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
and then
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"didDismissWithButtonIndex");
// If no text or wrong text show alert again otherwise normal actions
}
But if I press the cancel button, it 1st calls textFieldDidEndEditing which in turn calls the alert delegate. And again it calls the alert delegate method by itself.
So alert to be displayed is not getting shown and keyboard starts to pop up and goes back. So no alert is being shown in case when its to be shown.
If any doubts in the flow, please ask me.
How can I rectify the issue?
unset the delegate of the textField in alertView:willDismissWithButtonIndex:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
UITextField *textField = [alert textFieldAtIndex:0];
textField.delegate = nil;
}
When the alert is dismissed the textField will end editing, and afterwards it will call the textFieldDidEndEditing: delegate method.
If you set the delegate to nil before the dismissal starts, the delegate method can't be called.
Besides that, a better design would be to have a cancel button "Cancel", and an other button "Submit". When the textField ends you dismiss the alert with "Submit", and not "Cancel".
You simply want to hide UIAlertiView on cancel click?
Then if your cancel buttonindex is 1 then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
if(buttonIndex == 1) {
[[alert textFieldAtIndex:0] resignFirstResponder];
}
}
try this condition.. as you dont want alert to be gone when Pin is not there...
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if([textField.text length]!=0)
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
If you want to hide UIAlertiView on "Cancel" button click then just simply add this code to clickedButtonAtIndex:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
[[alert textFieldAtIndex:0] resignFirstResponder];
}
}
Try this
Add UIAlertView following delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
[[alert textFieldAtIndex:0] resignFirstResponder];
}

poptorootview when click on alerrtview ok

I have a home view ,when click on that it is going to another view again i am going to another view.when click on a button on that view a modalview will appear and then subsequently 3 more modal views when click on each modalview.when click on the final modalview an alert will appear and when click on that alert i want to show the root homeview.Is it possible
?
Display AlertView using given code snippet:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title message: #"Alert Message"
delegate: self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
Delegate Method implementation :
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Sample Code given below:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Alert Message?" message:#"Error......" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK"] autorelease];
[alert show];
The implemention alertView's delegate functions is given below
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//cancel clicked ...do your action
}
else if (buttonIndex == 1)
{
//OK clicked
[self.navigationController popToViewController animated:YES];
}
}
just give the delegate in .h file and after in delegate method of alertview write bellow code..
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[self.navigationController popToRootViewControllerAnimated:YES];///this line is important..
}
else{
// do your action...
}
}
i hope this answer is useful to you..
:)
int c=[self.navigationController.viewControllers count]-4;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:c] animated:YES];

Multiple UIActionSheets on the same delegate

I'm writing a puzzle game. When the user presses the check button, I see if the solution they entered is correct. Depending on the result, I present one of two action sheets for them. For now I just have some NSLog statements to make sure things are getting called, but only one of the sheets seems to work properly.
Nothing gets called when I click a button in showErrorsActionSheet. The action sheet disappears off the screen, but the logs never print.
I suspect it has something to do with having two actionsheets declared to the same delegate (self)
- (void) checkSolution {
//code determines the value of the BOOL allCorrect
if (allCorrect) { //IF ALL OF THE LETTERS WERE CORRECT
//display UIAlertView;
NSLog(#"allCorrect");
UIActionSheet *levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:#"Congratulations! You Have Finished the Level!" delegate:self cancelButtonTitle:#"Review my work" destructiveButtonTitle:#"Choose next puzzle" otherButtonTitles:nil, nil];
[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else {
//[self showIncorrectLettersInRed];
UIActionSheet *showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:#"Sorry, thats not right. Show errors in red?" delegate:self cancelButtonTitle:#"No Thanks, I'll keep trying" destructiveButtonTitle:#"Yes please, I'm stuck!" otherButtonTitles:nil, nil];
[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}
the methods that are supposed to be called are:
- (void) levelCompleteActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
NSLog(#"return to levelSelect");
//pushViewController:levelSelect
}
else {
NSLog(#"continue to examine solution");
}
}
- (void) showErrorsActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
NSLog(#"show errors in red");
}
else {
NSLog(#"continue to try");
}
}
and Ive declared the UIActionSheet protocol in the interface file as follows:
#interface GamePlay : UIViewController <UIActionSheetDelegate> {
Set a tag for each actionSheet, then use a switch statement in the UIActionSheet delegate.
Assign a tag
- (void)checkSolution
{
if (allCorrect)
{
UIActionSheet *levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:#"Congratulations! You Have Finished the Level!" delegate:self cancelButtonTitle:#"Review my work" destructiveButtonTitle:#"Choose next puzzle" otherButtonTitles:nil, nil];
[levelCompleteActionSheet setTag: 0];
[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else
{
UIActionSheet *showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:#"Sorry, thats not right. Show errors in red?" delegate:self cancelButtonTitle:#"No Thanks, I'll keep trying" destructiveButtonTitle:#"Yes please, I'm stuck!" otherButtonTitles:nil, nil];
[showErrorsActionSheet setTag: 1];
[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}
UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch ( actionSheet.tag )
{
case 0: /* levelCompleteActionSheet */
{
switch ( buttonIndex )
{
case 0: /* 1st button*/
break;
case 1: /* 2nd button */
break;
}
}
break;
case 1: /* showErrorsActionSheet */
break;
}
}
The same would apply anywhere else in this class as well, including levelCompleteActionSheet: and showErrorsActionSheet:. The only difference is, you would need to create an iVar for each actionSheet instead of creating them in checkSolution.
The methods that will be called by a UIActionSheet on its delegate are the methods listed in the UIActionSheetDelegate protocol.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html
To be called, your method must be one of those methods. I don't see levelCompleteActionSheet or showErrorsActionSheet listed in that protocol! :) Your method must be named actionSheet:clickedButtonAtIndex:, and not some name you make up out of whole cloth.
Using Tag solve this problem
levelCompleteActionSheet.tag = 100;
showErrorsActionSheet.tag = 101;
- (void) levelCompleteActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(actionSheet.tag == 100){
// levelCompleteActionSheet implement your required function
}
else if(actionSheet.tag == 101){
// showErrorsActionSheet implement your required function
}
}

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

UITableView and the alertView: clickedButtonAtIndex method

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)