UIWebView [web视图]

#import "ViewController.h"
#define width_screen self.view.bounds.size.width
#define height_screen self.view.bounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建一个文本框
    [self creatTextField];
    
    //创建button
    [self creatButton];
    
    [self createWebView];
}

#pragma mark    ------------------------textField
- (void)creatTextField
{
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 150, 50)];
    
    [self.view addSubview:tf];
    
    tf.borderStyle = UITextBorderStyleRoundedRect;
    
    tf.placeholder = @"http://www.baidu.com";
    
    tf.textColor = [UIColor orangeColor];
    tf.keyboardType = UIKeyboardTypeASCIICapable;
    
    tf.tag = 21;

}

#pragma mark    ------------------------button
- (void)creatButton
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 20, 60, 50)];
    
    [self.view addSubview:btn];
    
    btn.backgroundColor = [UIColor grayColor];
    
    [btn setTitle:@"Go" forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    //注册事件
    [btn addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];

}

-(void)onclick
{
    UIWebView *webW = (id)[self.view viewWithTag:20];
    
    UITextField *tf = (id)[self.view viewWithTag:21];
    
    NSURL *url = [NSURL URLWithString:tf.text];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [webW loadRequest:request];
    
    //放弃第一响应者
    [tf resignFirstResponder];
}//(0, 110, w_screen, h_screen-110)

#pragma mark    ------------------------web view
- (void)createWebView
{
    UIWebView *wv = [[UIWebView alloc]initWithFrame:CGRectMake(0, 70, width_screen, height_screen-110+40)];

    
    [self.view addSubview:wv];
    
    wv.tag = 20;
}


@end

posted @ 2015-09-21 22:02  阿凡提王  阅读(110)  评论(0编辑  收藏  举报