开发者

How can I load a subview separately?

I have a MainView with a lot of subviews! In one of that subview there is a WebView that check if there is a connection. The problem is that check connection ALWAYS at the startup of my app.. I want load this subview only when is opened...is that possible??

this is my code:

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;



}

The Webview is loaded trouhg a 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


If the main purpose of your web view is to check for connectivity, rather use the Reachability class provided by Apple to check for connectivity. Look for the Reachability sample app


You have the request generated in the webview's, viewDidLoad method which is called whenever the view loads.

You have a design problem here. the MainView shouldn't have outlets to its subviews. As you have it now, all the views will load whenever your MainView object is instantiated from the nib. That is what generates the request when your app launches.

Instead, the outlets should be in your viewController(UIViewController) object that controls your `MainView'. The viewController should load the subviews individually from nib as they are needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜