-1

我真的是 XCode 4.2 IOS 编程的新手。我已经有一个具有不同功能的项目(我的第一个项目),包括 UIWebView,它可以工作!直到..我尝试在没有网络时添加警报。

XCode 告诉我没有错误或问题。但是当我启动应用程序时,转到 WebView 所在的视图:

。H:

#import "ViewController.h"

@interface ReservierungsformularBHV : UIViewController

{
    IBOutlet UIWebView *webview;
}

-(IBAction)refreshClicks:(id)sender;
@property (weak, nonatomic) IBOutlet UIWebView *webview;

@end

米:

#import "ReservierungsformularBHV.h"

@interface ReservierungsformularBHV ()


@end

@implementation ReservierungsformularBHV
@synthesize webview;

- (void)viewDidLoad {

    NSURL *url = [NSURL URLWithString:@"http://www.bodega-online.com/tischreservierung      /mail_formular.php?ort=Bremerhaven"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [webview loadRequest:req];
    [super viewDidLoad];
}



-(void)awakeFromNIB{  //IGNORE
    [self refreshClicks:self]; //IGNORE
}
-(IBAction)refreshClicks:(id)sender{//IGNORE
     [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bodega-    online.com/tischreservierung/mail_formular.php?ort=Bremerhaven"]]];//IGNORE

     NSString *web = @"www.bodega-online.com/tischreservierung/mail_formular.php?ort=Bremerhaven";
     NSURL *url = [NSURL URLWithString:web];
     NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
     [webview loadRequest:requestUrl];

}
- (void)viewDidUnload
{
     [self setWebview:nil];
     [super viewDidUnload];
     // Release any retained subviews of the main view.

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:@"Please check your internet connection"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
     [alert show];

}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

XCode 暂停了模拟器并向我显示:

2013-04-29 09:39:18.164 Bodega[3139:c07] * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合键值编码的键 reservierungsformularBHV 。* First throw call stack: (0x142c012 0x1251e7e 0x14b4fb1 0xcfde41 0xc7f5f8 0xc7f0e7 0xca9b58 0x3b3019 0x1265663 0x142745a 0x3b1b1c 0x2767e7 0x276dc8 0x276ff8 0x277232 0x282c25 0x4823a3 0x27fee3 0x280167 0x2801a7 0x5ec0a2 0x5ddb99 0x5ddc14 0x1265705 0x1992c0 0x3d5a64 0x1265705 0x1992c0 0x199258 0x25a021 0x25a57f 0x2596e8 0x1c8cef 0x1c8f02 0x1a6d4a 0x198698 0x2530df9 0x2530ad0 0x13a1bf5 0x13a1962 0x13d2bb6 0x13d1f44 0x13d1e1b 0x252f7e3 0x252f668 0x195ffc 0x1d72 0x1ca5) libc++abi.dylib:终止调用抛出异常 (lldb)

我真的不知道该怎么办。对不起,我的英语,很久以前了。

有人可以帮我吗?我在这里找不到任何东西,我发现我已经在我的项目中使用过。我所做的一切,仍然来自教程。但是我想学!

4

1 回答 1

0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                            message:@"Please check your internet connection"
                                           delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];

您将委托设置为self,但未在 ReservierungsformularBHV.h 中定义 UIAlertView 协议

改变

@interface ReservierungsformularBHV : UIViewController

@interface ReservierungsformularBHV : UIViewController <UIAlertViewDelegate>

并在您的 ReservierungsformularBHV.m 添加以下内容:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog("Dismissing alert");
}

这是 UIAlertView 上苹果文档的链接:http: //developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

编辑:在您的 .h 文件更改

@interface ReservierungsformularBHV : UIViewController

@interface ReservierungsformularBHV : UIViewController <UIAlertViewDelegate, UIWebViewDelegate>

并将您的viewDidLoad功能更改为以下内容:

- (void)viewDidLoad {
    webview.delegate=self;
    NSURL *url = [NSURL URLWithString:@"http://www.bodega-online.com/tischreservierung      /mail_formular.php?ort=Bremerhaven"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [webview loadRequest:req];
    [super viewDidLoad];
}

让我解释一下这是做什么的:

webview 有一些所谓的委托函数,它们在不同的情况下被调用。您的webViewDidFail-function 从未被调用,因为您的 webview 没有分配正确的委托,这是通过在 .h 文件中定义 UIWebViewDelegate 协议并将正确的委托设置为 .hviewDidLoad来完成的self。您也可以让其他一些类处理您的 webview,但在这种情况下,您可以像上面解释的那样使用它。

无论如何,您应该使用可达性框架来检查活动的互联网连接,并在设备未连接时显示警报。有几个示例如何在网络上执行此操作。

于 2013-04-29T09:12:46.007 回答