我正在尝试创建一个对话框在我的iOS应用程序(应用程序是一个PhoneGap应用程序)。
我正在使用这篇文章中的代码:如何在iOS中实现弹出对话框
下面是链接
中的代码UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
message:@"You must be connected to the internet to use this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
我把这个代码放在AppDelegate.m
的方法
- (BOOL)application:(UIApplication)...
应用程序运行,但对话框不显示。
有什么问题吗?
我已经更新了代码如下
下面的代码在my appdelegate.m
//reachability code
if (networkStatus == ReachableViaWifi)
{
while (true)
//show progress bar
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
[alert show];
}
警告框没有显示。我做错了什么?
你可以在AppDelegate中显示UIAlertView如果你做的一切都是正确的,但是你不能使用exit()
方法退出应用程序,在iOS中自己退出应用程序是不正确的做法。基本上你的AppDelegate实现了UIApplicationDelegate协议,这个协议有很多方法。
尝试在AppDelegate的不同方法中显示AlertView。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
[alert show];
// other code...
}
OR
- (void)applicationDidBecomeActive:(UIApplication *)application {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
[alert show];
}
类似地,您可以根据您在问题中提到的可达性的状态更改相应地显示alertview。
我想这是因为你的代码在[self.window makeKeyAndVisible];
行以上。
尝试将alert代码移到该行之后。
您应该将代码添加到MainViewController中。m(或其他UIViewController .m文件),并根据你想要的行为代码应该添加到-(void)viewDidLoad
或-(void)viewDidAppear
方法。
对于iOS的警报对话框,在你的AppDelegate中。m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
message:@"You must be connected to the internet to use this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
[self.window makeKeyAndVisible];
return YES;
}
既然你提到你正在使用phonegap,他们会在不同的平台上为你处理警告框。您可以在其中一个页面html
中使用以下代码function showAlert() {
navigator.notification.alert(
'You must be connected to the internet to use this app.',
null,
'No network connection',
'OK'
);
}