swift 游乐园。创建项目级别的测试

在创建的时候选择

swift 游乐园。创建项目级别的测试_第1张图片

 

创建后结构

swift 游乐园。创建项目级别的测试_第2张图片

修改代码

 

MyPlayground

//: A UIKit based Playground for presenting user interface
  
import UIKit
import PlaygroundSupport

let vc = ViewController1()
let navigationVc = UINavigationController(rootViewController: vc)

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = navigationVc;

ViewController1

import Foundation
import UIKit


public class ViewController1:UIViewController{
    public override func viewDidLoad(){
        super.viewDidLoad()
               // Do any additional setup after loading the view.
        let button = UIButton(type: .custom)
        button.frame=CGRect(x:0,y: 200,width: 300,height: 50)
        button.setTitle("点击跳转", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .blue
        button.addTarget(self, action: #selector(onButtonClick), for: .touchUpInside)
        view.addSubview(button)
     
    }
    @objc func onButtonClick() {
         navigationController?.pushViewController(ViewController2(), animated: true)
     }

}

ViewController2

import Foundation
import UIKit


public class ViewController2:UIViewController{
    public override func viewDidLoad(){
        super.viewDidLoad()
       let view = UIView()
             view.backgroundColor = .white

             let label = UILabel()
             label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
             label.text = "Hello World!"
             label.textColor = .black
             
             view.addSubview(label)
             self.view = view
    }
}

 

你可能感兴趣的:(#,swift)