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!?