iOS对话框无法正常工作

时间:2013-04-24 21:20:37

标签: ios objective-c mobile alert

我正在尝试在我的iOS应用中创建一个对话框(app是一个PhoneGap应用程序)。

我正在使用此帖子中的代码:how to implement a pop up dialog box in 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)...

应用程序运行但对话框未显示。

有什么问题?


我已更新以下代码

以下代码位于我的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];
}

警告框未显示。我做错了什么?

4 个答案:

答案 0 :(得分:1)

你可以在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... 
}

- (void)applicationDidBecomeActive:(UIApplication *)application {

   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
    [alert show];
}

同样,您可以根据问题中提到的可更新性状态更改显示相应的警报视图。

答案 1 :(得分:0)

我想这是因为您的代码位于第[self.window makeKeyAndVisible];行之上。

尝试在该行之后移动代码以进行提醒。

答案 2 :(得分:0)

您应该将代码添加到MainViewController.m(或其他UIViewController .m文件)中,并根据您希望的行为将代码添加到-(void)viewDidLoad-(void)viewDidAppear方法中。

答案 3 :(得分:0)

对于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'                  
    );
}
相关问题