Versuchen Sie den Code aus der ersten Antwort in der Verbindung (von Kampai) zu verwenden: How to use UIAlertController to replace UIActionSheet?dismissViewController Abschluss-Handler nicht aufgerufen
Allerdings ist die Abschluss-Handler nicht einmal in meinem Code genannt.
Das Warnmeldungs-Aktionsblatt kann nach dem Drücken beider Schaltflächen geschlossen werden, aber nichts innerhalb des Beendigungshandlers funktioniert. Irgendeine Idee, was könnte das Problem sein? Ich bin neu in der Verwendung von Complete Handler und habe versucht, Antworten online zu finden, aber nur wenige haben das gleiche Problem wie ich.
- (IBAction)takePhotoButtonPressed:(UIButton *)sender {
pressedButtonTagNumber = sender.tag;
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Take a Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"!");
// Take a Photo button tapped
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"0"); // NOT CALLED
// Initialize UIImagePickerController
UIImagePickerController *takePhotoImagePickerController = [[UIImagePickerController alloc] init]; takePhotoImagePickerController.delegate = self;
takePhotoImagePickerController.allowsEditing = YES;
NSLog(@"1");
// Check and assign image source
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"2");
UIAlertController *noCameraErrorSheet = [UIAlertController alertControllerWithTitle:@"Camera is not available" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[noCameraErrorSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
} else {
takePhotoImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
// Present UIImagePickerController
[self presentViewController:takePhotoImagePickerController animated:YES completion:NULL];
}
}];
}]];
Lösung:
@ Paulw11 Lösung funktioniert gut:
1) Keine Notwendigkeit, dismissViewController für UIAlertController. 2) Kann nicht einen neuen UIAlertController aufrufen, wenn der eine Wrapping es (offenkundig) ablehnt. 3) Überprüfen und deaktivieren Sie die Schaltfläche im Voraus.
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}]];
UIAlertAction *takePhotoActionButton = [UIAlertAction actionWithTitle:@"Take Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self takePhoto];
}];
UIAlertAction *uploadPhotoActionButton = [UIAlertAction actionWithTitle:@"Upload from Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self uploadPhoto];
}];
// Disable take a photo button if source not available
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[takePhotoActionButton setEnabled:FALSE];
}
// Disable upload a photo button if source not available
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
[uploadPhotoActionButton setEnabled:FALSE];
}
[actionSheet addAction:takePhotoActionButton];
[actionSheet addAction:uploadPhotoActionButton];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
Rufen Sie 'disneyViewController: animated:', um den UIAlertController zu entlassen? Wenn ja, tu das nicht. Es wird automatisch ausgeblendet, wenn eine Schaltfläche angetippt wird. Im Aktionshandler müssen Sie nur den Code schreiben, den Sie ausführen möchten, wenn die Aktionsschaltfläche ausgewählt ist. Z.B. Ihre Aktion "Abbrechen" wäre leer – Paulw11
Danke! Das habe ich auch gerade herausgefunden. Ich frage mich nur, wann ich "[self disneyViewControllerAnimated: YES completion:^{ }];" Warum funktioniert dieser Completion-Handler nicht? –
Sie verwenden das immer dann, wenn Sie einen View-Controller ablehnen möchten. Der Grund, warum Sie es hier nicht verwenden, ist, dass 'UIAlertController' sich bereits selbst abgetan hat. Angenommen, Sie wollten eine Zeitüberschreitung, bei der der Alarm nach 5 Sekunden abgebrochen wurde, wenn der Benutzer keine Option ausgewählt hat. Sie könnten einen Timer starten und nach 5 Sekunden 'disagleViewController: animated:' aufrufen, um den Alarm zu entfernen. – Paulw11