Swift's first IOS Program (Browsing web Pages)

Source: Internet
Author: User

Swift's first IOS Program (Browsing web Pages)

1. Project Establishment

 

A. Select the second project (Greate a new Xcode project)

 

B. Select Application in IOS on the left, Singl View Application on the right, and next

 



C. Enter the program Name in Product Name, such as fistIOS

D. Select the program storage location


2. Start Encoding
A. File description AppDelegate. the swift file is the file that the IOS project responds to the entire APP. AppDelegate is used to listen to events of the entire APP (For details, refer to the final code); ViewController. swift is mainly used to write code for the control view. Main. storyboard, which turns the UI visualization tool into a storyboard;


B. Select Main. storyboard. In the middle view, click wAny hAny to select the corresponding size as shown in, and double-click it.

C. You can see the addition of the control in the lower right corner (drag it directly) and add the Button and WebView controls respectively.


D. As shown in the upper right corner, click the icon in the second circle to see Step e. The Code will appear on the right of the view.


E. Associate the UI with the view control and right-click the Button control. The following menu is displayed:

Find the Touch Down menu item, and there is a plus Button on the right side of the menu item. Drag it to the code area with the mouse selected, name the Button, and click connect.

Right-click the WebView control, find the New Referecing Outlet menu item, drag the same method to the blank area of the code, and name the control webview.


F. ViewController. swift: Add code to the function button.



3. Run the code
(Note: The simulator may be too large. When you see the white screen, you can pull it up to see the content)


Click the browse Web button to view the corresponding content on the web page.




Code comments for each file:
Implement a Web page browsing program 1. AppDelegate. swift (application proxy, listening for the entire APP event) // AppDelegate. swift // First_IOS /// Created by Emperor on 15/3/5. // Copyright (c) 2015 Emperor. all rights reserved. // import UIKit // AppDelegate is used to listen to events of the entire APP. For example, after the APP is started, the application method @ UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow? // Call func application (application: UIApplication, didfinishlaunchingwitexceptions launchOptions: [NSObject: AnyObject]?) after the APP is started. -> Bool {return true} func applicationWillResignActive (application: UIApplication) {}// call func applicationDidEnterBackground (application: UIApplication) when the APP enters the background) {}// call func applicationWillEnterForeground (application: UIApplication) {} func applicationDidBecomeActive (application: UIApplication) {}// call func applicationterminwillate (application: UIApplication) Upon APP termination) {}} 2. Main. storyboard visualizes the UI The tool becomes a storyboard. Add a Button and WebView control here. 3. ViewController. swift (View Controller) // ViewController. swift // First_IOS /// Created by Emperor on 15/3/5. // Copyright (c) 2015 Emperor. all rights reserved. //// function: implement a program that can browse Web pages. // It is mainly used to write control view code. This file implements a ViewController class, which is a subclass of UIViewController and is used to control the UI of the view. Two event methods are generated by default in this class: import UIKitclass ViewController: UIViewController {/* by default, Main. storyboard and ViewController. swift is associated, so you can write the code of the control UI in ViewController. in the swift file. In ViewController. in swift, right-click a web control, for example, and a menu will pop up. Find the "New Referecing Outlet" item, and there is a plus sign on the right side of it, drag the mouse to the blank area of the ViewController code area (six icons are displayed on the rightmost side of xcode. Select the second icon to view the associated code of the view ); if you press the button, select the Touch Down option * // added webview variable, which can directly access the WebView control (add it to Main by yourself. storyboard story panel) @ IBOutlet weak var webview: UIWebView! // Press the button to do the thing here @ IBAction func onTouchDown (sender: AnyObject) {var url = NSURL (string: http://blog.csdn.net/emperorzhi/article/details/44049985); var request = NSURLRequest (URL: url !); Webview. loadRequest (request);} // www. bkjia. comoverride func viewDidLoad () {super. viewDidLoad () // Do any additional setup after loading the view, typically from a nib .} // when the memory is insufficient, override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}


 

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.