iOS development--Press and hold two Uialertview solutions

Source: Internet
Author: User

Sometimes we want to delete some data by long-press gestures, which is a good user experience is to pop up a uialertview before deleting to remind the user to make two confirmations. However, this will cause a bug: The Uialertview you define will pop up two times, such as


The code is as follows:

Add gestures section:

    SELF.LONGPRESSGR = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (handlelongpress:)];    [Self AddGestureRecognizer:self.longPressGR];

Gesture Method:

-(void) Handlelongpress: (Uilongpressgesturerecognizer *) recognizer {    if (self.delegate = = nil) {        return;    }        Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "Are you sure you want to delete this course? "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];    [Alert show];}

Uialertview's Delegate

-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex {    if (Buttonindex = = 1) {        [self deleteData];}    }

The reason for this is that we all know that gestures have the State property, each gesture has a begin,recognized,ended status, and Uilongpressgesturerecognizer is a "long process" gesture, Its specific state change and trigger method timing is difficult, so an effective and simple way is to add judgment in the trigger method, only when the BEGIN state triggers this method, so it can solve the

-(void) Handlelongpress: (Uilongpressgesturerecognizer *) recognizer {    if (recognizer.state = = Uigesturerecognizerstatebegan) {        if (self.delegate = = nil) {            return;        }                Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "Are you sure you want to delete this course? "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];        [Alert show];}    }

:



Attach the status of the gesture and the state and timing of the method that can be triggered in these states. From Apple's UIGestureRecognizer.h file.

    Uigesturerecognizerstatepossible,//The recognizer have not yet recognized it gesture, but could be evaluating touch Events. The the default state Uigesturerecognizerstatebegan,//The recognizer have received touches recognized as The gesture.  The action method is called at the next turn of the run loop uigesturerecognizerstatechanged,//the recognizer Have received touches recognized as a change to the gesture.  The action method is called at the next turn of the run loop uigesturerecognizerstateended,//the recognizer has received touches recognized as the end of the gesture. The action method is called at the next turn of the run loop and the recognizer would be the reset to Uigesturerecognizers Tatepossible uigesturerecognizerstatecancelled,//The recognizer have received touches resulting in the cancellation O f the gesture. The action method is called at the next turn of the run loop. The recognizer would be a reset to UigesturerecOgnizerstatepossible uigesturerecognizerstatefailed,//The recognizer have received a touch sequence that can n OT be recognized as the gesture. The action method won't be called and the recognizer would be reset to uigesturerecognizerstatepossible//Discre Te Gestures–gesture recognizers that recognize a discrete event but does not report changes (for example, a tap) does not tr Ansition through the began and Changed states and can not fail or be cancelled uigesturerecognizerstaterecognized = UIG esturerecognizerstateended//The recognizer has received touches recognized as the gesture. The action method is called at the next turn of the run loop and the recognizer would be the reset to Uigesturerecognizers Tatepossible



iOS development--Press and hold two Uialertview solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.