ios - How can I close UIAlertController without button -
i use uialertcontroller , uiactivityindicatorview loading indicator, , close until app gets server response. when wrong server or network app never response, can't control alertview, want know there method can close alertview touching screen. , uialertview without title , buttons. hints thankful.
it crashed when touch screen, here code:
uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:nil message:@"正在装载页面...\n\n" preferredstyle:uialertcontrollerstylealert]; uiactivityindicatorview *spinner = [[uiactivityindicatorview alloc]initwithactivityindicatorstyle:uiactivityindicatorviewstylewhitelarge]; spinner.center = cgpointmake(130.5, 65.6); spinner.color = [uicolor darkgraycolor]; [spinner startanimating]; [alert.view addsubview:spinner]; uitapgesturerecognizer *recognizer = [[uitapgesturerecognizer alloc]initwithtarget:alert action:@selector(stopwaiting)]; [alert.view addgesturerecognizer:recognizer];
save alert controller property (for example, alertcontroller
) , close
[self.alertcontroller dismissviewcontrolleranimated:yes completion:nil];
to touching alert add tap gesture recognizer alertcontroller's view:
- (void)showalert { uialertcontroller *alertcontroller = [uialertcontroller alertcontrollerwithtitle:@"" message:@"some message" preferredstyle:uialertcontrollerstylealert]; uitapgesturerecognizer *tapgesturerecognizer = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(closealert)]; [alertcontroller.view addgesturerecognizer:tapgesturerecognizer]; self.alertcontroller = alertcontroller; [self presentviewcontroller:alertcontroller animated:yes completion:nil]; } - (void)closealert { [self.alertcontroller dismissviewcontrolleranimated:yes completion:nil]; }