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