问题描述
- 求助:如何禁用Alertview弹出
- 我的应用里面有一个Alertview,没有网络连接的时候会弹出来。但是我希望有网络连接的时候它可以自动禁用。代码如下:
-(void)reachabilityChanged:(NSNotification*)note { Reachability * reach = [note object]; if([reach isReachable]) { notificationLabel.text = @""Notification Says Reachable""; NSLog(@""Internet is Up""); } else { notificationLabel.text = @""Notification Says Unreachable""; NSLog(@""Internet is Down""); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""Please connect to Internet"" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125 50 30 30)]; progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; [alert addSubview:progress]; [progress startAnimating]; [alert show]; } }
解决方案
可以把alertView当做实体变量,然后在ivar调用didDismissWithButtonIndex,然后就可以在viewDidLoad中分配alert,然后:
-(void)reachabilityChanged:(NSNotification*)note{ Reachability * reach = [note object]; if([reach isReachable]) { notificationLabel.text = @""Notification Says Reachable""; NSLog(@""Internet is Up""); [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:0]; } else { notificationLabel.text = @""Notification Says Unreachable""; NSLog(@""Internet is Down""); UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125 50 30 30)]; progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; [alert addSubview:progress]; [progress startAnimating]; [alert show]; } } -(void)dismissAlert:(UIAlertView *)alertView{ [alertView dismissWithClickedButtonIndex:0 animated:YES]; }
在头文件里执行UIAlertViewDelegate。
时间: 2024-11-01 08:54:45