|
I'm not able to trigger an Alert View from the onDelete closure, invoked deleting a list element, for example:
Any suggestion ? Thanks in advance. |
|
Hi @giurobrossi,
I think that a. views cannot be inserted via the
That would require you to introduce a |
|
That's fine, but the alert is shown after the execution of the onDelete callback: my purpose is to show the alert before the deletion of a list element. Unfortunately the indexSet of the deleting element is know only by the onDelete callback. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
I'm using CoreData. The template for Master / Detail with CoreData has this in the Event.swift
The content view has this:
So I would think you can make your own function for deleting (a class function in User), and only go there if the user agrees from the alert. And put the alert in .onDelete like you did. |
|
EDIT: actually this should be even easier with
this way you don't have to set the boolean and you also spare the force unwrap of the Optional (see here for reference: https://www.hackingwithswift.com/books/ios-swiftui/using-alert-and-sheet-with-optionals) (btw. it's nice to see how I can improve my own answer due to learning more stuff 🥳) original answer: Hi @giurobrossi, I've solved this issue (not having the indexSet after
in my struct and setting this in the
Hope that helps :) (and I'm feeling great right now because this is the first time ever I shared my SwiftUI knowledge 🥰) Greetings, Kevin |
|
Here is how I solved this. I wanted to write
Here's how I implemented it:
|
|
You may want to think about UX. As
or
So add another check might not be the best. If you just had a Button that delete then setting a 2nd check in case of accidental touches is a good idea but |
|
@NigelGee it's not fully right: the swipe gesture can be done in one gesture: swipe from right to far left. If you don't stop your finger in the middle, then there is no second step needed. If you say the user has two steps because he could stop in the middle, it's like saying that when pressing a button there is never need for confirmation because the user could, before releasing the button, swipe out of the button to cancel his tap... for data that are important, relying on such subtle and quick decision is too weak. On my side I used a similar solution as @wolfmcnally, but i'm facing a wrong animation: I swipe > the row disapear (as if was deleted, while I did not already deleted it from the ForEach source) and reapears immediately > My alert opens > I confirm > the row is removed looking at your comment, you don't seems to notice such behavior ? |
|
I'm correct in swipe to delete that the user has to make a long swipe to delete until red fills the whole of the row and if user moves back (before lifting finger) then it not deleted. A two step still With regards to your data coming back after delete depending on the way your data is saved and without some code will not be able to tell but it sounds like that it not remove from the data set |
|
then you are agree that pushing a button is also a two steps action: press the button, and release it. and if the user wants to cancel he moves his finger out of the button before releasing ? For me a real two steps action can only be called like this if we force the user to raise the finger in between. Else the second action can be done too quickly (immediately), without having the time for revalidating what he is doing. about the problem more related to this thread, here is the section of my code:
as you see, I delete the item only in the alert code... but when swiping it, the item disapear and reapears immediately (it then diseapars correctly agin and definitively when confirming the deletion in alert) |
|
Hi That is the correct way the If you want to have this I would look at Hope that helps Nigel |
|
Even Apple applications are requesting an explicit confirmation when you are about to delete definitively an important data. you can go for instance to vocal recordings app: if you delete the recording, it moves to a recently deleted section. If you go to this section and swipe to delete definitively the recording, there is a popup for confirming the deletion. About my problem, the above example add an animation at the end of the swipe as if the row was deleted and immediately reapears. That's my problem. If you look at the apple vocal recording app, the row has not this glitch. So I assume there is a way to proceed to avoid it. |
|
Yes that is true but if you noticed that the recording is still there untill you tap the Delete Forever button in the ActionSheet. which is the behave that you have in your app. i thought you did not want this that way I suggested useing another way but if that what you want then no problem. You will also notice that if you just tap on the recording that you can Recover it from Recently Deleted. |
|
no that's not the behavior i have in my app... as said, in my app, the row disapears (quick animation where the row heigt reduces to 0, while red background fills all the line, even if don't drag my finger far at the left) and reapears (imediate animation) while the alert opens. when looking the apple voice recording app, this animation glitch does not occurs. |
|
Hi I was having a think about it and maybe do.... When
and if the user This should when swipe to delete disappear from the I know not like the "Voice Recording" app but if you want that I think you will have to build your own Swipe to Delete method. Nigel PS not sure if it will work as not tested it but it might a option to explore. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.