Swift UI special training 25 URL input Complete Event

Source: Internet
Author: User



First, let's run through the program written in the previous sentence to see what the problem is.









We find that clicking return is unresponsive, because we have previously set up a text box for the first responders of this interface, and we want to get the keyboard back to do something with the keyboard. Starting with TextField, add a method:


func textFieldShouldReturn(textField: UITextField) -> Bool {
       textField.resignFirstResponder()
        return true
        
    }

This is a way to click the Back button input box to disappear, in which we have to remove the TextField's first responders before returning. Now we need to add the method of accessing the URL in the browser, call the previously defined Loadurl method, the above method is modified as follows:
func textFieldShouldReturn(textField: UITextField) -> Bool {
      loadurl(textField.text, web: web1)
        textField.resignFirstResponder()
        return true
        
    }

Called the Loadurl method, there are two parameters, the first is the URL, which we entered in the Search field URL, the other is rendered WebView, only one, before we dragged over the web1. It is also important to note that the full URL needs to have http://, but we do not want to enter this part of the input, it is necessary to make minor changes in the Loadurl method, plus the prefix of the address.


 func loadurl(url:String ,web:UIWebView){
    let aurl = NSURL(string: "http://" + url)
        let urlrq = NSURLRequest(URL: aurl!)
        web.loadRequest(urlrq)
    
    }

The complete controller code is as follows:
//
// ViewController.swift
// WebBrowser
//
// Created by Chen Gang on 15/2/28.
// Copyright (c) 2015 cg. All rights reserved.
//

Import UIKit

Class ViewController: UIViewController, UIWebViewDelegate, UITextFieldDelegate{
    @IBOutlet weak var loading: UIActivityIndicatorView!
    @IBOutlet weak var textField1: UITextField!

    @IBOutlet weak var web1: UIWebView!
    Override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    / / Load web page request
    Func loadurl(url:String ,web:UIWebView){
    Let aurl = NSURL(string: "http://" + url)
        Let urlrq = NSURLRequest(URL: aurl!)
        web.loadRequest(urlrq)
    
    }
    //The page starts loading
    Func webViewDidStartLoad(webView: UIWebView) {
        loading.startAnimating()
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    }
    / / page end loading
    Func webViewDidFinishLoad(webView: UIWebView) {
        loading.stopAnimating()
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    }
    Override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    Func textFieldShouldReturn(textField: UITextField) -> Bool {
      Loadurl(textField.text, web: web1)
        textField.resignFirstResponder()
        Return true
        
    }
}

Now let's run the following look at the effect:



Enter a URL with a long access time and click the return button



We can see that the small gears we set in the reading process and the small gears in the upper left-hand corner of the system are turning the cue process.




Complete, it can be used normally.



Swift UI special training 25 URL input Complete Event


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.