2009-11-08 41 views
0

我有一个MainView有很多子视图! 在其中一个子视图中有一个WebView,用于检查是否存在连接。问题是,检查连接始终在我的应用程序启动..我只想打开时加载此子视图...是否有可能?如何分别加载子视图?

这是我的代码:

MainView.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 

@class VistaUno; 
@class VistaDue; 
@class Vistatre; 
@class risposta; 
@class VistaQuattro; 

@interface MainView : UIView { 

    IBOutlet VistaUno *vistaUno; 

    IBOutlet VistaDue *vistaDue; 

    IBOutlet Vistatre *vistaTre; 

    IBOutlet risposta *Risposta; //this is the view that I want to load separately 

    IBOutlet VistaQuattro *vistaQuattro; 



} 

Risposta.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 

@class MainView; 


@interface risposta : UIView <UIWebViewDelegate>{ 

    IBOutlet MainView *mainView; 



} 

web视图加载trouhg一个UIViewController ....

WebViewControllerRisposta.h

#import <UIKit/UIKit.h> 


@interface WebViewControllerRisposta : UIViewController <UIWebViewDelegate>{ 


    IBOutlet UIWebView *webview2; 

    IBOutlet UIActivityIndicatorView *m_activity; 
} 

@property (nonatomic, retain) UIActivityIndicatorView *m_activity; 
@property (nonatomic, retain) UIWebView *webview2; 


@end 

WebViewControllerRisposta.m

#import "WebViewControllerRisposta.h" 


@implementation WebViewControllerRisposta 



- (void)viewDidLoad { 

    NSString *urlAddress = @"http://www.google.it"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [webview2 loadRequest:requestObj]; 

} 



#pragma mark UIWebView delegate methods 

- (void)webViewDidStartLoad:(UIWebView *)webview2 { 

    m_activity.hidden= FALSE; 
    [m_activity startAnimating]; 

    NSLog(@"Web View Did started loading..."); 

} 



- (void)webViewDidFinishLoad:(UIWebView *)webview2 { 

    m_activity.hidden= TRUE; 
    [m_activity stopAnimating]; 

    NSLog(@"Web View finish loading"); 


    //Ricordarsi di aggiungere il codice per eliminare l'acquisto 

} 

- (void)webView:(UIWebView *)webview2 didFailLoadWithError:(NSError *)error { 
    [m_activity stopAnimating]; 
    m_activity.hidden= TRUE; 

    NSLog(@"Error %i", error.code); 
    if (error.code == NSURLErrorCancelled) return; // this is Error -999 
    // error handling for "real" errors here 

    if (error != NULL) { 
     UIAlertView *errorAlert = [[UIAlertView alloc] 
            initWithTitle:@"NETWORK ERROR" 
            message:@"Sembra che al momento non vi è una connessione dati attiva! Per scaricare i nuovi Enigmi o inviare la tua risposta è necessaria una connessione Internet!" 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
     [errorAlert show]; 
     [errorAlert release]; 


    } 
} 





/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [webview2 dealloc]; 
    [super dealloc]; 
} 


@end 

回答

0

您在网络视图的方法viewDidLoad中生成请求,该方法在视图加载时调用。

这里有一个设计问题。 MainView不应该有其子视图的网点。就像你现在所做的那样,只要从你的笔尖实例化了MainView对象,所有的视图都会加载。这是您的应用启动时生成请求的原因。

相反,插座应该位于控制您的MainView的viewController(UIViewController)对象中。 viewController应该根据需要从nib单独加载子视图。

+0

谢谢!我知道了! – Obliviux 2009-11-08 20:30:55

1

如果您的Web视图的主要目的是检查连接,而是使用由苹果公司提供的可达性类检查连接。查找可达性示例应用程序