如何单独加载子视图?

时间:2009-11-08 12:53:37

标签: iphone objective-c

我有一个包含很多子视图的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;



}

Webview加载了一个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

2 个答案:

答案 0 :(得分:1)

如果您的网络视图的主要目的是检查连接,而是使用Apple提供的Reachability类来检查连接。寻找Reachability示例应用

答案 1 :(得分:0)

您在webview的viewDidLoad方法中生成了请求,只要视图加载,就会调用该方法。

这里有设计问题。 MainView不应该有子视图的出口。正如您现在所拥有的那样,只要从nib实例化MainView对象,就会加载所有视图。这就是应用程序启动时生成请求的原因。

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