I just started iOS development, and now I'm stuck. I have a table cell, where the user cann see data. Next to it, there are two buttons. One to delete, one to edit. The edit part is where I'm stuck. I have a label field. When the user clicks on the edit button a prompt should come where the user can type in data. I already found something to do it. But there's no save button and I don't know how I could then save the variable to an NSString
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Name der neuen Liste" message:#"" delegate:self cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
I currently have this. lNow a prompt pops up, where I can write something, but not save. I just can press cancel afterwards. How do I work with user input in iOS? I don't want to use the label as an UITextField since it has to be clickable to perform another action.
Try this:
- (void)noteInput
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"input"
message:#"input"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *noteText = [alertView textFieldAtIndex:0];
NSString *note = noteText.text;
}
}
Try adding a Save button in otherButtonTitles. Then, in your UIAlertViewDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Save button pressed
}
}
Related
I Created Password Validation. Password created successfully it will show one UIAlertView and press when user presses the OK, the Password Text field will get clear. How to do that? Please any Example?
Add UIAlertViewDelegate protocol:
#interface YourViewController : UIViewController <UIAlertViewDelegate>
Show Alert View using :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE_HERE"
message:#"ALERT_MESSAGE HERE."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
After user presses OK, this method will called automatically:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
// do something here...
yourTextField.text = nil;
}
}
Your question is not clear. If you want the password field text to be empty after displaying the UIAlert, please add the following code to make your password filed empty.
yourTextField.text = ""
Here's an example.
if(passwordValid==YES)
{
UIAlertView *passwordValidated = [[UIAlertView alloc] initWithTitle:#"Password Validated!"
message:#"Your password is successfully validated. Press OK to continue"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
yourTextField.text = "";
[updateMessage show];
I have searched far and wide for an answer but I haven't found anything close.
The following code:
UIAlertView *welcome = [[UIAlertView alloc] initWithTitle:#"Welcome"
message:#"Please Enter Your Credentials to Proceed"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[welcome show];
Gives the following error:
textFieldIndex (0) is outside of the bounds of the array of text fields
If I change the alertView type to UIAlertViewStylePlainTextInput then it works and I cant figure out why!
Any help would be appreciated.
Somewhere you are calling -textFieldAtIndex: on an alertView object that does not or is not meant to have a textField.
Check for things like:
[welcome textFieldAtIndex:0];
or...
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//...
[alertView textFieldAtIndex:0];
//...
}
This could be if you're using multiple alertView objects and haven't handled done case-handling to bifurcate the implementation for an alertView object that has a textField and another alertView that doesn't have a textField
I have a table with some names loaded from a plist and what I'm trying to do is basically remove the one selected by the user and everything is working fine, but what I'm trying to do that I cannot figure out is basically give the user the option of cancel or continue with the deletion using a UIAlertView.
This is what I have that obviously doesn't work and it deletes the user regardless of what button was touched.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// This is fine since here I'm only notifying the user that guest cannot be deleted
UITableViewCell *celectedRow = [self.tableScores cellForRowAtIndexPath:indexPath];
NSString *removeUserWithKey = celectedRow.textLabel.text;
if ([removeUserWithKey isEqual: #"Guest"])
{
// don't remove anything and show message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Information!"
message:#"The user 'Guest' cannot be removed."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
else
{
// the problem I have is here, how can I stop deleting and saving
// if user selects the cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Information!"
message:#"The user 'Guest' cannot be removed."
delegate:self
cancelButtonTitle:#"Continue"
otherButtonTitles:#"Cancel", nil];
[alert show];
//IF USER NAME IS OTHER THAN GUEST REMOVE IT
// remove selected row from table
[arrayRecords removeObjectAtIndex:indexPath.row];
// remove selected user from dictionary
[dictionaryCopyOfRecordsFromPlist removeObjectForKey:removeUserWithKey];
// write dictionary to plist after removing items
[self.pListReader writeToPlist:#"studentsRecords.plist" withDictionary:dictionaryCopyOfRecordsFromPlist];
// reload items in table to reflect any changes made
[self.tableScores reloadData];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Continue"])
{
NSLog(#"Continue.");
}
else if([title isEqualToString:#"Cancel"])
{
NSLog(#"Cancelled");
}
}
Again, this code works fine if I wouldn't want to give the user the option of cancelling the deletion.
How can I structure my code in a way that I can cancel the deleting process if user selects the cancel button?
Any suggestion?
You need to make sure you remove the object from the array only when the user presses continue button. Move the code for removal of guest to alert view delegate
Tag the alert view with the row number.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Information!"
message:#"The user 'Guest' cannot be removed."
delegate:self
cancelButtonTitle:#"Continue"
otherButtonTitles:#"Cancel", nil];
alert.tag = indexPath.row;
[alert show];
Remove the record in Alert view delegate.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Continue"])
{
NSLog(#"Continue.");
//Remove the corresponding row.
[arrayRecords removeObjectAtIndex:alertView.tag];
// remove selected user from dictionary
[dictionaryCopyOfRecordsFromPlist removeObjectForKey:removeUserWithKey];
// write dictionary to plist after removing items
[self.pListReader writeToPlist:#"studentsRecords.plist" withDictionary:dictionaryCopyOfRecordsFromPlist];
// reload items in table to reflect any changes made
[self.tableScores reloadData];
}
else if([title isEqualToString:#"Cancel"])
{
NSLog(#"Cancelled");
//Do Nothing
}
}
I want the user to not be able to click the cancel button on UIAlertView, yet I still want it to be there, but shaded out. I know a shouldEnableFirstButton function is there but it doesn't involve cancel button. Do you have any idea how to do it?
Thanks.
Use the following:
UIAlertView *vw = [[UIAlertView alloc] initWithTitle:#"Information" message:#"This is a message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
[vw show];
So, essentially you are setting the "Cancel" as other button.
Now, the following will solve your problem:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return NO;
}
If you want to perform some action in OK button press you can always do that here:
- (void)alertViewCancel:(UIAlertView *)alertView
I'd like to selectively enable the 'save' button on my UIAlertView so that you can't save a file with no name. I can listen to text changed events for the attached UITextView (the style is UIAlertViewStylePlainText), but I can't see how to access the buttons so that I can do the enable/disable. I've tried iterating through [alertView subviews], but there is only the label in there (no buttons). Where do I need to look to directly access the UIButtons attached to a UIAlertView?
Simple, just implement the UIAlertViewDelegate in your class and utilize the alertViewShouldEnableFirstOtherButton: delegate method. You can use this to check the length of the text field, and enable the button accordingly...
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return [[[alertView textFieldAtIndex:0] text] length] > 0;
}
Make sure you set your view controller to conform to this delegate in your interface using < UIAlertViewDelegate > and to set this class as the alerts delegate upon instantiation.
Instantiate your alertview:
UIAlertView *av = [UIAlertView alloc] initWithTitle:#"my title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
});
Then make sure your class implements UIAlertViewDelegate, and look for the button index of the button you want to listen to:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
//save here
}
}
0x7fffffff posted right answer, but if someone is looking for answer how to add that textfield and save button to UIAlertView, here is the answer:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Save" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
This delegate method:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
return [[[alertView textFieldAtIndex:0] text] length] > 0;
}
won't work if you add that save button with addButtonWithTitle: method.
And when save button is clicked, this delegate method is called and here you can read what user wrote to that text field:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1) {
// Save button clicked
NSLog(#"%#", [[alertView textFieldAtIndex:0] text]);
}
}
You can't access the buttons of an alert view as of iOS 7. Unfortunately they made the subviews completely hidden and there's no way to access them.