嗨我在iOS的Xcode项目中遇到了我的 - (void)问题。 首先,这里是代码
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UIWebView *webview;
IBOutlet UIActivityIndicatorView *active;
UIAlertView *alert_start;
UIAlertView *alert_error;
}
-(IBAction)tele_button:(id)sender;
-(IBAction)mail_button:(id)sender;
-(IBAction)web_button:(id)sender;
-(IBAction)news_button:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//Stop Bounce for WebView
for (id subview in webview.subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
//First Start Alert
[alert_start show];
NSLog(@"first alert");
NSString *start_alert = [[NSUserDefaults standardUserDefaults] objectForKey:@"alert_start"];
if(start_alert == nil)
{
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert_start"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *alert_start = [[UIAlertView alloc] initWithTitle:@"iOptibelt"
message:@"On some points this application need a internet connection."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert_start show];
[alert_start release];
}
// Do any additional setup after loading the view, typically from a nib.
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"home-de" ofType:@"html"]isDirectory:NO]]];
NSLog(@"webview fertig");
}
-(void)webViewDidStartLoad:(UIWebView *) webview {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[active startAnimating];
NSLog(@"lade");
}
-(void)webViewDidFinishLoad:(UIWebView *) webview {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[active stopAnimating];
NSLog(@"fertig");
}
-(void)webView: (UIWebView *) webview didFailLoadWithError:(NSError *)error{
NSLog(@"lade error");
UIAlertView *alert_error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect. Please check your internet Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert_error show];
[alert_error release];
};
- (IBAction)tele_button:(id)sender{
NSLog(@"it's connected!");
//Local HTML Call Button
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"phone" ofType:@"html"]isDirectory:NO]];
[webview loadRequest:theRequest];
}
- (IBAction)mail_button:(id)sender{
NSLog(@"it's connected!");
//Mail App Mail Button
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://info@optibelt.com"]];
}
- (IBAction)web_button:(id)sender{
NSLog(@"it's connected!");
//Local HTML Button
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://optibelt.com"]];
[webview loadRequest:theRequest];
}
- (IBAction)news_button:(id)sender{
NSLog(@"it's connected!");
//local Home Button
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"home-de" ofType:@"html"]isDirectory:NO]];
[webview loadRequest:theRequest];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
最后我的3. - (无效)不起作用,我不知道可能是什么问题....
-(void)webViewDidStartLoad:(UIWebView *) webview {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[active startAnimating];
NSLog(@"lade");
}
-(void)webViewDidFinishLoad:(UIWebView *) webview {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[active stopAnimating];
NSLog(@"fertig");
}
-(void)webView: (UIWebView *) webview didFailLoadWithError:(NSError *)error{
NSLog(@"lade error");
UIAlertView *alert_error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect. Please check your internet Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert_error show];
[alert_error release];
答案 0 :(得分:0)
您需要设置此行代码
webView.delegate = self;
可能在您的ViewDidLoad方法
中答案 1 :(得分:0)
将粘贴的代码作为文字和总代码,问题是语法错误。第一次粘贴邮件时,在结束后}
有一个不应该出现的分号。第二次,你错过了结束}
,它应该在那里。代码应该是:
-(void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
NSLog(@"lade error");
UIAlertView *alert_error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect. Please check your internet Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert_error show];
[alert_error release];
}
答案 2 :(得分:0)
首先我必须说,你的问题写得非常糟糕,据我所知,你的webView方法不起作用。
您需要将您的webView delegate
设置为Bobj-C提及。
webView.delegate = self;
其次,您需要将UIWebViewDelegate
添加到头文件(.h)
@interface ViewController : UIViewController <UIWebViewDelegate> {
}
答案 3 :(得分:0)
在@jrturton回答后,你说UIWebviewDelegate的方法被调用了,但是
是的......修好了loadingViews ......只是错误警报不起作用......再次
如果代码中的Error aler
是alert_start
UIAlertView,可能是因为您在首次显示[NSUserDefaults standardUserDefaults]
后更新alert_start
。
如果Error alert
是alert_error
UIAlertView,jrturton是对的,您需要确保您的UIWebView无法加载您指定的文件:
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"home-de" ofType:@"html"]isDirectory:NO]]];