如何在单击第一个操作 sheet、Swift 中的按钮时显示第二个操作 sheet
How to show second action sheet on clicking a button in first action sheet, Swift
我可以通过调用 showActionSheetToEditPhoto()
来显示 editPhotoActionSheet
。
当我在 editPhotoActionSheet
中单击删除按钮时,我想显示 deletePhotoActionSheet
,但我无法使用以下代码执行此操作。
请告诉我如何实现这一目标。
另请参阅下面的两个操作表。
func showActionSheetToEditPhoto() -> UIAlertController {
let alertController : UIAlertController = UIAlertController()
let takePhoto : UIAlertAction = UIAlertAction(title: "Take Photo", style: .default) { (alert) in
print("User pressed Take Photo")
self.takePhoto()
}
let choosePhoto : UIAlertAction = UIAlertAction(title: "Choose Photo", style: .default) { (alert) in
print("User pressed Choose Photo")
self.choosePhoto()
}
let editPhoto : UIAlertAction = UIAlertAction(title: "Edit Photo", style: .default) { (alert) in
print("User pressed Edit Photo")
self.editExisitingProfilePhoto()
}
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed Delete Photo")
self.showDeletePhotoActionSheet()
}
let cancel : UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in
print("User pressed Cancel")
}
alertController.addAction(takePhoto)
alertController.addAction(choosePhoto)
alertController.addAction(editPhoto)
alertController.addAction(deletePhoto)
alertController.addAction(cancel)
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
return alertController
}
func showDeletePhotoActionSheet() -> UIAlertController {
print("showOptionsToDeletePhoto function called")
self.dismiss(animated: true, completion: nil) // Dismissing previous alert
let alertController : UIAlertController = UIAlertController()
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed delete Photo")
var existingPhoto = self.profilePhoto.image
if existingPhoto != nil{
self.profilePhoto.image = nil
}
}
let cancel : UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in
print("User pressed Cancel")
}
alertController.addAction(deletePhoto)
alertController.addAction(cancel)
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
return alertController
}
EditPhotoActionSheet
DeletePhotoActionSheet
您的方法 showDeletePhotoActionSheet
return UIAlertController
实例,但您尚未使用该对象来呈现操作 sheet。因此,请在您的 deletePhoto 操作中显示操作警报。
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed Delete Photo")
let alert = self.showDeletePhotoActionSheet()
//present delete the action sheet
self.present(alert, animated: true) //Or self.present(self.showDeletePhotoActionSheet(), animated: true)
}
我可以通过调用 showActionSheetToEditPhoto()
来显示 editPhotoActionSheet
。
当我在 editPhotoActionSheet
中单击删除按钮时,我想显示 deletePhotoActionSheet
,但我无法使用以下代码执行此操作。
请告诉我如何实现这一目标。
另请参阅下面的两个操作表。
func showActionSheetToEditPhoto() -> UIAlertController {
let alertController : UIAlertController = UIAlertController()
let takePhoto : UIAlertAction = UIAlertAction(title: "Take Photo", style: .default) { (alert) in
print("User pressed Take Photo")
self.takePhoto()
}
let choosePhoto : UIAlertAction = UIAlertAction(title: "Choose Photo", style: .default) { (alert) in
print("User pressed Choose Photo")
self.choosePhoto()
}
let editPhoto : UIAlertAction = UIAlertAction(title: "Edit Photo", style: .default) { (alert) in
print("User pressed Edit Photo")
self.editExisitingProfilePhoto()
}
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed Delete Photo")
self.showDeletePhotoActionSheet()
}
let cancel : UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in
print("User pressed Cancel")
}
alertController.addAction(takePhoto)
alertController.addAction(choosePhoto)
alertController.addAction(editPhoto)
alertController.addAction(deletePhoto)
alertController.addAction(cancel)
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
return alertController
}
func showDeletePhotoActionSheet() -> UIAlertController {
print("showOptionsToDeletePhoto function called")
self.dismiss(animated: true, completion: nil) // Dismissing previous alert
let alertController : UIAlertController = UIAlertController()
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed delete Photo")
var existingPhoto = self.profilePhoto.image
if existingPhoto != nil{
self.profilePhoto.image = nil
}
}
let cancel : UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in
print("User pressed Cancel")
}
alertController.addAction(deletePhoto)
alertController.addAction(cancel)
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
return alertController
}
EditPhotoActionSheet
DeletePhotoActionSheet
您的方法 showDeletePhotoActionSheet
return UIAlertController
实例,但您尚未使用该对象来呈现操作 sheet。因此,请在您的 deletePhoto 操作中显示操作警报。
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed Delete Photo")
let alert = self.showDeletePhotoActionSheet()
//present delete the action sheet
self.present(alert, animated: true) //Or self.present(self.showDeletePhotoActionSheet(), animated: true)
}