代码注释算详细的,主要看代码,理论跟OC一样。。
导航栏
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
var Vc = ViewController()
var nav = UINavigationController.init(rootViewController:Vc)
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
ViewController 里面代码
//
// ViewController.swift
// SwfitTestClass
//
// Created by TL on 2017/7/18.
// Copyright © 2017年 TL. All rights reserved.
//
import UIKit
/**Delegate 主要思路步骤
- 1.那个地方要传值,哪里写代理! (总的来说,是那个需要招人就在哪里写)
- 2.自己要把delegat,要检查是否有人已经弄好deleagte,愿意来做代理
- 3.遵守协议的这方要做的事情,就是遵守协议,实现协议方法!(这个是对于传值的来说的! 不牵扯代理概念)
*----
**/
protocol DiceGame:NSObjectProtocol {
func play()
}
class ViewController: UIViewController,GetmessageDelegate {
//字段
public var _button:UIButton?
public var _label:UILabel?
public var Myview:UIView!
private var datasouce:Array<String> = Array<String>()
override func viewDidLoad() {
super.viewDidLoad()
/* MARK ---self
self.Myview = UIView()
self.Myview.frame = CGRect.init(x: 100, y: 100, width: 100, height: 100)
self.Myview.backgroundColor = UIColor.red
self.view.addSubview(Myview)
*/
//设置title
self.title = "RootViewContrlooer"
//初始化,设置大小
_label = UILabel.init(frame: CGRect.init(x: 50, y: 100, width: 300, height: 44))
//设置显示文字
_label?.text = "get message form next page"
//设置BG
_label?.backgroundColor = UIColor.cyan
//这里一定要解包
self.view.addSubview(_label!)
/*
1.创建button
2.UIControlState.normal
*/
_button = UIButton.init(frame: CGRect.init(x:50,y:200,width:200,height:44))
_button?.setTitle("go to next page", for: UIControlState.normal)
_button?.setTitleColor(UIColor.yellow, for: UIControlState.normal)
_button?.backgroundColor = UIColor.blue
//如果方法没有实现直接报错
_button?.addTarget(self, action: #selector(nextpage), for: UIControlEvents.touchUpInside)
self.view.addSubview(_button!)
}
//跳转下一个界面
func nextpage(){
let NextVc = NextController()
NextVc.delegate = self
self.navigationController?.pushViewController(NextVc, animated: true)
}
//实现代理方法不实现代理不上去
func GetMesgssage( string: String) {
_label?.text = string
if string == "" {
_label?.text = "null"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
NextController 代码
//
// NextController.swift
// SwfitTestClass
//
// Created by TL on 2017/7/18.
// Copyright © 2017年 TL. All rights reserved.
//
import Foundation
import UIKit
//设置代理!写代理
protocol GetmessageDelegate:NSObjectProtocol {
func GetMesgssage(string:String)
}
//
class NextController: UIViewController {
//设置一个delegate字段
var delegate:GetmessageDelegate?
// 申请UItextField
var _textField:UITextField?
override func viewDidLoad() {
super.viewDidLoad()
//设置当前的navgation的title
self.title = "SecondViewController"
//设置view的颜色
self.view.backgroundColor = UIColor.white
//初始化textfield
_textField = UITextField.init(frame: CGRect(x: 60, y: 100, width: 200, height: 44))
//设置风格
_textField?.borderStyle = UITextBorderStyle.roundedRect
//设置提示语
_textField?.placeholder = "input sth to send back mesage"
self.view.addSubview(_textField!)
//设置mybutton
var Mybutton = UIButton.init(frame: CGRect(x:60,y:200,width:200,height:44))
//设置中心位置
Mybutton.center = CGPoint.init(x: 160, y: 200)
//设置Title
Mybutton.setTitle("send message back", for: UIControlState.normal)
//添加事件
Mybutton.addTarget(self, action: #selector(goBack), for: UIControlEvents.touchUpInside)
//设置颜色
Mybutton.backgroundColor = UIColor.blue
//增加button
self.view.addSubview(Mybutton)
}
func goBack() {
if delegate != nil {
delegate?.GetMesgssage( string: _textField!.text!)
self.navigationController?.popViewController(animated: true)
}
}
}