The problem of Uialertview in multi-threading

Source: Internet
Author: User

The scene is like this, click the button, open a new thread, pop Alertview. Then call Dismisswithclickedbuttonindex to make it disappear.

1 /** Define button*/2-(void) setupallbtns{3UIButton *btn2 =[UIButton Buttonwithtype:uibuttontyperoundedrect];4Btn2.frame = CGRectMake ( -, $, -, -);5[Btn2 Settitle:@"Reading Information"Forstate:uicontrolstatenormal];6 [btn2 Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];7Btn2.backgroundcolor =[Uicolor Browncolor];8 [btn2 addtarget:self Action: @selector (Readsimwithnewthread) forcontrolevents:uicontroleventtouchupinside];9 [Self.view addsubview:btn2];Ten } One  A-(void) readsimwithnewthread{ -Nsthread *button3thread = [[Nsthread alloc] initwithtarget:self selector: @selector (Readinfo)Object: nil]; -[Button3thread SetName:@"thread-3"]; the [Button3thread start]; - } -  - /** New thread read information*/ +-(void) readinfo{ -[[Nsoperationqueue Mainqueue] addoperationwithblock:^ +      { ASelf.malertview = [[Uialertview alloc] Initwithtitle:nil message:@"start reading, please wait ..." Delegate: Self Cancelbuttontitle:nil otherbuttontitles:nil, nil]; at [Self.malertview show]; -      }]; -      -     Charsn[ -] = {0}; -     intRET =[Self.sim GETINFO:SN]; -      in     //method of alert disappearing -     if(self.mAlertView.visible) { to[Self.malertview Dismisswithclickedbuttonindex:0Animated:yes]; +     } -      theNSString *msg =@""; *Msg=[nsstring stringWithFormat:@"mobilenum=%@", [NSString STRINGWITHUTF8STRING:SN]]; $}

But sometimes the alert interface always appears, causing the interface to die.
The reason for this is that the alertview is not drawn when the call alert disappears when the thread is blocked by multithreading. When Alertview is drawn out, the method of alert disappearance is obsolete.
The workaround is to have the thread pause for a period of time when the method that the alert disappears is executed. such as 0.5s

[Nsthreadsleepfortimeinterval:0.5

or with

[Self performselector: @selector (Dimissalert) Withobject:alert Afterdelay:2.0]; // add it after [Self.malertview show] -(void) dimissalert{    if  (alert.visible) {        [alert dismisswithclickedbuttonindex:0  animated:yes];}    }

This way, after a fixed time, alert will always disappear.

The problem of Uialertview in multi-threading

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.