关闭

斯坦福课程学习笔记

399人阅读 评论(0) 收藏 举报
分类:

1.ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    self.view.alpha = 0.4;

    

    NSString *link = @"<a href=\"http://apple.com\">Apple</a>";

    NSString *html = [NSString stringWithFormat:@"Go to %@",link];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(20, 60, self.view.frame.size.width-40, 30)];

    webView.delegate = self;

    [webView loadHTMLString:html baseURL:nil];

    [self.view addSubview:webView];

    // Do any additional setup after loading the view, typically from a nib.

}


-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {

        WebViewController *webViewController = [[WebViewController alloc] init];

        webViewController.url = request.URL;

        [self presentViewController:webViewController animated:YES completion:nil];

        return NO;

    }

    return YES;

}


2.WebViewController

-(void)loadURL

{

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    [webView loadRequest:request];

    txtField.text = url.absoluteString;

}


-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    self.url = [NSURL URLWithString:textField.text];

    [self loadURL];

    [textField resignFirstResponder];

    return YES;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    txtField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width-40, 20)];

    txtField.delegate = self;

    txtField.backgroundColor = [UIColor greenColor];

    txtField.textColor = [UIColor redColor];

    [self.view addSubview:txtField];

   

    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height-84)];

    webView.delegate = self;

    [self.view addSubview:webView];

    

    //textField.text = url.

    // Do any additional setup after loading the view.

}


-(void)webViewDidFinishLoad:(UIWebView *)webView

{

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}


-(void)viewWillAppear:(BOOL)animated

{

    if (!self.url) {

        self.url = [NSURL URLWithString:txtField.text];

    }

    

    [self loadURL];

}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:40114次
    • 积分:895
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:80篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论