-(недействительно)... не работает / не нравится (iOS)
Привет, у меня проблема с моим -(void) в моем проекте Xcode для iOS. Прежде всего, вот код
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) не работает, и я больше не представляю, в чем может быть проблема....
-(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];
4 ответа
Вам нужно установить эту строку кода
webView.delegate = self;
может быть, в вашем методе ViewDidLoad
Принимая ваш вставленный код как буквальный и общий код, проблема заключается в синтаксической ошибке. Когда вы в первый раз вставили сообщение, после закрытия у вас будет точка с запятой }
это не должно быть там. Во второй раз вам не хватает закрытия }
, который должен быть там. Код должен быть просто:
-(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];
}
После ответа @jrturton вы сказали, что методы UIWebviewDelegate вызываются, но затем
да... хорошо исправил loadViews... просто предупреждение об ошибке не работает... больше
Если 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]]];
Прежде всего я должен сказать, что ваш вопрос написан очень плохо, насколько я понимаю, ваши методы webView не работают.
Вы должны установить свой веб-вид (ы) delegate
как упоминает Bobj-C.
webView.delegate = self;
Во-вторых, вам нужно добавить UIWebViewDelegate
в ваш заголовочный файл (.h)
@interface ViewController : UIViewController <UIWebViewDelegate> {
}
Также; Возможное дублирование: iPhone - webViewDidFinishLoad не вызывается