#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
web