iOS对话框不工作
我想在我的iOS应用程序(应用程序是PhoneGap应用程序)中创建一个对话框。iOS对话框不工作
我使用这个职位代码: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];
}
警告框没有显示出来更新的代码如下
下面的代码是。我究竟做错了什么?
我想这是因为你的代码在行[self.window makeKeyAndVisible];
以上。
尝试在该行之后移动您的代码以提醒。
您应该将代码添加到您的MainViewController.m(或其他UIViewController .m文件)中,并取决于您想要将代码添加到-(void)viewDidLoad
或-(void)viewDidAppear
方法中的行为。
我希望能够在我的appdelegate中调用对话框,所以我该怎么做。从你回答我的理解是,我会把UIAlerView * alert = .....代码放在 - (void)viewDidLoad或 - (void)viewDidAppear方法中,我会在我的应用程序委托中使用[alert show ] ;.我的解释是否正确? – aagarwal 2013-04-24 22:02:33
您可以在方法内添加UIAlertView代码:' - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions'。从我在你的评论中看到的内容中添加了代码 - (void)applicationDidFinishLaunching:(UIApplication *)application'这个方法用于iOS 3.0的早期版本,你应该使用第一种方法。 – danypata 2013-04-24 22:53:16
对于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'
);
}
您可以显示在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];
}
同样你可以为你的可达性状态的变化相应地显示你在问题中提到的alertview。
请参阅编辑。你的第一个代码示例不起作用。 – aagarwal 2013-04-24 22:39:44
你的第一个样本意味着什么不起作用?第二是工作?尝试在初始化窗口后移动它。 – nsgulliver 2013-04-24 22:42:58
我从来没有尝试过第二个例子。当我说第一个样本不工作时,我的意思是没有任何反应。警告框不显示。 – aagarwal 2013-04-25 13:13:00
从哪个应用程序显示AlertView?显示您的完整代码。 – nsgulliver 2013-04-24 22:10:15
@nsgulliver是不是应用程序的方法? – aagarwal 2013-04-24 22:21:51
AppDelegate正在实现一个名为[UIApplicationDelegate](http://developer.apple)的协议。com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html),这个委托有很多方法可以在委托中看到。他们开始**应用程序** – nsgulliver 2013-04-24 22:24:59