UIWebView的创建使用

来源:互联网 发布:什么美工刀片好 编辑:程序博客网 时间:2024/05/21 16:38

注:本次只是自定义前进后退按钮,并非有html解析 请谅解

如果需要更多具体的知识,请登录:http://blog.csdn.net/iunion/article/details/7963291


1.是什么东西?

UIWebView是iPhone自带的浏览器功能

2.干什么用的?

在开发中可以调用此功能完成浏览网页的功能,可以自定义前进后退按钮。

3.代码实例

.h文件

#import <UIKit/UIKit.h>

#import "WaitingView.h"

#import "HeadDefine.h"

@interface WebViewController :SuperViewController<UIWebViewDelegate>

{

    UIWebView *webv;

    WaitingView *waiting;

}

-(void)LoadData:(NSString *)url;

@property(nonatomic,strong)id<ViewcontrlDelegate>delegate;

@end

.m文件

#import "WebViewController.h"

@interfaceWebViewController ()

@end


@implementation WebViewController

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

    if (self) {

        webv=[[UIWebViewalloc]initWithFrame:CGRectMake(0,0 ,UI_SCREEN_WIDTH,VIEW_HEIGHT+VIEW_START_Y)];

        webv.delegate=self;

        webv.autoresizingMask =UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;

        webv.scalesPageToFit =NO;

        [self.viewaddSubview:webv];

        waiting=[[WaitingViewalloc]initWithFrame:CGRectMake(0,VIEW_START_Y,UI_SCREEN_WIDTH,UI_SCREEN_HEIGHT)];

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

}

-(void)LoadData:(NSString *)url

{

    [webvloadRequest:[NSURLRequestrequestWithURL:[NSURL URLWithString:url]]];

}

#pragma mark -

#pragma mark - Deleagte


- (void)webViewDidStartLoad:(UIWebView *)webView

{

    [UIApplicationsharedApplication].networkActivityIndicatorVisible =YES;

    [self.viewaddSubview:waiting];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    [UIApplicationsharedApplication].networkActivityIndicatorVisible =NO;

    [waitingremoveFromSuperview];

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

    [UIApplicationsharedApplication].networkActivityIndicatorVisible =NO;

    [waitingremoveFromSuperview];

}

#pragma mark -


-(void)viewWillAppear:(BOOL)animated

{

    [delegatechangViewAppear];

}

-(void)viewWillDisappear:(BOOL)animated

{

    [delegatechangViewDisappear];

}

@end

---------------------------------------------------------------------------------------------------------------------------------------------

另外一个组代码    与上面没有关系的

.h

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

#import "HeadDefine.h"

@interface capital : UIViewController<UIWebViewDelegate>

{

    UIWebView *webview;        //定义UIWeb

    UILabel *textlabel;        //label

}

@property(assign,nonatomic)NSString *strTitle,*strURL;        //传过来的url地址


-(void)setFunc;             //运行方法

@end


.m

#import "capital.h"


@implementation capital

//@synthesize delegate;

@synthesize strTitle,strURL;



- (id)init

{

    self = [super init];

    if (self) {


        webview = [[UIWebViewalloc] initWithFrame:CGRectMake(0,  (IOS_V<7.0)?(UI_TAB_BAR_HEIGHT):(VIEW_START_Y),UI_SCREEN_WIDTH, (IOS_V<7.0)?(UI_VIEW_HEIGHT+UI_TAB_BAR_HEIGHT):(UI_SCREEN_HEIGHT-VIEW_START_Y))];

        webview.backgroundColor = [UIColorblackColor];

        webview.autoresizingMask =UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        webview.delegate =self;

        webview.scalesPageToFit =NO//自动占满屏幕

        [self.view addSubview:webview];


        UISegmentedControl *SegmentedControl = [[UISegmentedControlalloc] initWithItems:

                                                [NSArrayarrayWithObjects:

                                                 [UIImageimageNamed:@"ie_prev_disable.png"],

                                                 [UIImageimageNamed:@"ie_next_disable.png"],

                                                  nil]];           //按钮的图片  前进后退

        [SegmentedControl addTarget:selfaction:@selector(segmentAction:)    //前进按钮

                   forControlEvents:UIControlEventValueChanged];

        SegmentedControl.frame = CGRectMake(0, 0, 100, 40);

        SegmentedControl.segmentedControlStyle =UISegmentedControlStyleBar;

        SegmentedControl.momentary = YES;

        SegmentedControl.backgroundColor=[UIColorclearColor];

        SegmentedControl.tintColor = [UIColorblackColor];


        UIBarButtonItem *segmentBarItem = [[UIBarButtonItemalloc]

                                           initWithCustomView:SegmentedControl];

        //添加 segmentedControl


        UIToolbar *toolbar = [[UIToolbaralloc] initWithFrame:CGRectMake(0.0,0.0, UI_SCREEN_WIDTH, 44.0)];           //工具条

        toolbar.barStyle =UIBarStyleBlack;

        [toolbar sizeToFit];


        textlabel = [[UILabelalloc] initWithFrame:CGRectMake(100,5, 120,35)];

        textlabel.font = [UIFontfontWithName:@"ArialMT"size:20];

        textlabel.textAlignment =UITextAlignmentCenter;

        textlabel.textColor = [UIColorwhiteColor];

        textlabel.backgroundColor=[UIColorclearColor];

        textlabel.tag=2013;


        UIBarButtonItem *closeButtom = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCanceltarget:selfaction:@selector(bark)];  //链接ui表层

        closeButtom.style =UIBarButtonItemStyleBordered;

        UIBarButtonItem *filespace = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];

        

        NSArray *items = [NSArrayarrayWithObjects:segmentBarItem,filespace ,closeButtom, nil];

        toolbar.items = items;


        [toolbar addSubview:textlabel];

        [self.view addSubview:toolbar];

    }

    return self;

}


- (void)segmentAction:(id)sender

{

    if ([sender selectedSegmentIndex] ==0) {

        [webview goBack];


    }else if ([senderselectedSegmentIndex] == 1) {

        [webview goForward];

    }

}


-(void)setFunc

{

    textlabel.text =strTitle;  //label文字显示

    NSRange rang=NSMakeRange(0,4);

    if ([[strURLsubstringWithRange:rang] isEqualToString:@"http"]) {

        [webviewloadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]]];

    }

    else

    {

        NSString *path = [[NSBundlemainBundle] pathForResource:@"happyshakehelp_EN"ofType:@"html"];

        NSURL *url = [NSURLfileURLWithPath:path];

        NSURLRequest *request = [NSURLRequestrequestWithURL:url];

        [webview loadRequest:request];

    }

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


#pragma mark- MyMethod


- (void)bark

{

    [selfdismissModalViewControllerAnimated:YES];

    if ([UIApplicationsharedApplication].networkActivityIndicatorVisible) {

        [UIApplication sharedApplication].networkActivityIndicatorVisible =NO;

    }

}

-(void)back

{

    [webview goBack];

}


#pragma mark - Deleagte


- (void)webViewDidStartLoad:(UIWebView *)webView

{

    [UIApplication sharedApplication].networkActivityIndicatorVisible =YES;

}

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    [UIApplication sharedApplication].networkActivityIndicatorVisible =NO;

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

    [UIApplication sharedApplication].networkActivityIndicatorVisible =NO;

}

- (void)alertWithTitle:(NSString*)title Message:(NSString*)message ButtonTitle:(NSString*)btnStr

{

    UIAlertView *alertview =[[UIAlertViewalloc]initWithTitle:title

                                                      message:message

                                                     delegate:self

                                            cancelButtonTitle:btnStr

                                            otherButtonTitles:nil];

    [alertview show];

}


- (NSUInteger) supportedInterfaceOrientations{

    returnUIInterfaceOrientationMaskPortrait;

}


- (BOOL) shouldAutorotate {

    return NO;

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

}

-(void)SingleTap:(UITapGestureRecognizer*)recognizer

{

    NSLog(@"clicke");

}

-(void)viewWillAppear:(BOOL)animated

{

    [[NSNotificationCenterdefaultCenter] postNotificationName:@"appearTab"object:nil]; //NSNotificationCenter是专门供程序中不同类间的消息通信而设置的.

}

-(void)viewDidDisappear:(BOOL)animated

{

     [[NSNotificationCenterdefaultCenter] postNotificationName:@"hideTab"object:nil];  //NSNotificationCenter是专门供程序中不同类间的消息通信而设置的.

}


@end



原创粉丝点击