擔(dān)心表述不清楚,直接上代碼:https://gitee.com/xgkp/necessaryAlert.git
以下是簡單流程:
- 創(chuàng)建一個UsefulAlertView繼承于UIview,得到一個UsefulAlertView.swift文件
2.Xib創(chuàng)建一個同名的UsefulAlertView.xib
并且放置一個Button
3.綁定UsefulAlertView.swift 和 UsefulAlertView.xib
注意綁定控件的時候要注意選擇區(qū)域:如下圖
Button的視圖和UsefulAlertView.swift綁定
Button的點擊事件和UsefulAlertView.swift綁定
- UsefulAlertView.swift里的代碼初始化并設(shè)置點擊事件
//
// UsefulAlertView.swift
// newproject
//
// Created by Pro on 2018/1/17.
// Copyright ? 2018年 Pro. All rights reserved.
//
import UIKit
class UsefulAlertView: UIView {
@IBOutlet weak var TestDisplayBtn: UIButton!
var ConnectView:UIView!
var buttonCallBack:(() -> ())?
override init(frame: CGRect) {
super.init(frame: frame)
ConnectView = loadViewFromNib()
addSubview(ConnectView)
addConstraints()
initialSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
ConnectView = loadViewFromNib()
addSubview(ConnectView)
addConstraints()
initialSetup()
}
func loadViewFromNib() -> UIView {
let className = type(of: self)
let bundle = Bundle(for: className)
let name = NSStringFromClass(className).components(separatedBy: ".").last
let nib = UINib(nibName: name!, bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
func addConstraints() {
ConnectView.translatesAutoresizingMaskIntoConstraints = false
var constraint = NSLayoutConstraint(item: ConnectView, attribute: .leading,
relatedBy: .equal, toItem: self, attribute: .leading,
multiplier: 1, constant: 0)
addConstraint(constraint)
constraint = NSLayoutConstraint(item: ConnectView, attribute: .trailing,
relatedBy: .equal, toItem: self, attribute: .trailing,
multiplier: 1, constant: 0)
addConstraint(constraint)
constraint = NSLayoutConstraint(item: ConnectView, attribute: .top, relatedBy: .equal,
toItem: self, attribute: .top, multiplier: 1, constant: 0)
addConstraint(constraint)
constraint = NSLayoutConstraint(item: ConnectView, attribute: .bottom,
relatedBy: .equal, toItem: self, attribute: .bottom,
multiplier: 1, constant: 0)
addConstraint(constraint)
}
//初始化默認(rèn)屬性配置
func initialSetup(){
TestDisplayBtn.backgroundColor = UIColor.red
}
@IBAction func AlertBtnAction(_ sender: UIButton) {
if buttonCallBack != nil {
buttonCallBack!()
}
}
}
5.Viewcontroller中的調(diào)用和點擊事件的賦值
//
// ViewController.swift
// newproject
//
// Created by Pro on 2018/1/17.
// Copyright ? 2018年 Pro. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
let myAlert = UsefulAlertView(frame: CGRect(x: 0 , y: 0, width: UIScreen.main.bounds.width , height: UIScreen.main.bounds.height))
myAlert.buttonCallBack = {() -> () in
myAlert.removeFromSuperview()
print("click it")
}
self.view.addSubview(myAlert)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
6.具體按鈕點擊事件可以用block,target,delegate實現(xiàn),本文選擇用的是block,如果需要自己用target和delegate寫女阀,請參考以下第二個鏈接。
參考來源:http://www.hangge.com/blog/cache/detail_1394.html
參考來源:http://blog.csdn.net/syg90178aw/article/details/47020097
參考來源:http://www.reibang.com/p/b8a46bc55373