Reputation: 621
In my application when a user clicks on the save button in the toolbar, the user is then prompted through UIAlertView for which way they would like to save their current work by choosing either to save as an image or save as a play. When the user selects save as a play they are then prompted with a 2nd UIAlertView which also has a text field for them to insert the name for the play. What I am trying to achieve is so that when no text is inputted, the Ok button is disabled and when the length of the text typed in is 1 or more, the file is then able to be saved (using archiver, this works correctly so this is not an isue) and the Ok button is then enabled. Listed below is the code that shows the two alert views, as well as what happens when different items from the views are selected.
- (IBAction)selectSaveType {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@""
message:@"Please Select an Option."
delegate:self
cancelButtonTitle:@"Save Play"
otherButtonTitles:@"Save to Photos", nil];
[message show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Save Play"])
{
NSLog(@"Save Play was selected.");
[self GetFileName];
}
else if([title isEqualToString:@"Save to Photos"])
{
NSLog(@"Save To Photos was selected.");
//here is where we need to find how to call saveDrawing.
[self saveDrawing];
}
else if([title isEqualToString:@"Ok"])
{
NSLog(@"OK selected");
UITextField *fName= [alertView textFieldAtIndex:0];
NSString *NameFile = fName.text;
[self savePlay:NameFile];
}
}
-(void)savePlay:(NSMutableString *)fileName{
//code here to save via archive.
NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [pathforsave objectAtIndex:0];
//here we need to add the file extension onto the file name before we add the name to the path
[fileName appendString:@".hmat"];
NSString *strFile = [documentDirectory stringByAppendingPathComponent:fileName];
[NSKeyedArchiver archiveRootObject:matView.drawables toFile:strFile];
}
I have been trying to use the following code below to handle this, but when the first UIAlertView fires (which is the asking to select a play - no text field present) - the function below then is ran and crashes the application since there are no text fields in the first alert view.
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
return YES;
}
else
{
return NO;
}
}
alertViewShouldEnableFirstOtherButton is being hit when the first alert fires, then my application crashes in the simulator. Does anyone see why this would happen? Two things I am not so sure of
One - why the handle for the Ok button on the 2nd alert view to name the play is handled in the same block where the other buttons are handled. Since its a separate alert view, shouldn't it be in its own block?
Two - why alertViewShouldEnableFirstOtherButton is hit when it hasn't gotten to the 2nd alert view yet, it is called and runs with the first alert view, which crashes the app.
Thanks for your help, I am new to objective C.
Upvotes: 2
Views: 3152
Reputation: 130222
Delegate methods for an alert view will be called for any alert view you present. That being said this code will crash because textFieldAtIndex:0
does not exist on a plain alert view. To solve this all you need to do is add an if statement to the delegate method identifying which alert called the action.
Edit: No longer identifies alert by name. Code now checks the style of the delegates sender.
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput) {
if([[[alertView textFieldAtIndex:0] text] length] >= 1 )
{
return YES;
}
else
{
return NO;
}
}else{
return YES;
}
}
Upvotes: 10