Changes to the UI direct test support in the SWIFT3 playground

Source: Internet
Author: User
Tags uikit

We know that not only can the console code be tested in Xcode's playground, but the UI code can be tested, and even we can test the scene in Spritekit

In this article we simply talk about the changes playground have made to Uikit support in the latest Xcode8.0 Beta2 (hereinafter referred to as XCODE8B2).

First, post the following small snippet demo code:

1
Import uikit
import xcplayground

//2
class Responder:nsobject {

  func tap () {
    print (" Button pressed ")
  }
} let
responder = responder ()

//3 let
button = UIButton (type:. System)
button.settitle ("button", Forstate:. Normal)
Button.addtarget (Responder, Action: tap), forControlEvents:. Touchupinside)
button.sizetofit ()
button.center = Cgpoint (x:50, y:25)

//4 let
frame = CGRect (x:0 , y:0, width:100, height:50) let
view = UIView (frame:frame)
view.addsubview (button)
XCPlaygroundPage.currentPage.liveView = view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18-------------19 20 21 22 23 24 25 1 2-3 4 5 6, 7 23 24 25

Importing Uikit is a must, and importing Xcplayground is to test Uikit code in the UI! But unfortunately the above code is not very good in the xcode8b2 run, this is because the last sentence will be an error!

The solution is simply to import the Playgroundsupport library at the beginning of the playground and then change the last sentence to:

PlaygroundPage.current.liveView = view
1 1

Finally, don't forget to open the UI test interface:

All right, so we can see a button in the Xcode right, which is the oil we created in the playground with the code.

Click the button before you bind the tap method:

If you feel dissatisfied, you can immediately change the UI interface in playground code and then see the result immediately without creating a new Xcode UI project, is it convenient!?

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.