版本记录
版本号 | 时间 |
---|---|
V1.0 | 2020.05.23 星期六 |
前言
iOS中的视图加载可以有两种方式,一种是通过xib加载,另外一种就是通过纯代码加载。它们各有优点和好处,xib比较直观简单,代码比较灵活但是看着很多很乱,上一家公司主要风格就是用纯代码,这一家用的就是xib用的比较多。这几篇我们就详细的介绍一个xib相关知识。感兴趣的可以看上面写的几篇。
1. xib相关(一) —— 基本知识(一)
2. xib相关(二) —— 文件冲突问题(一)
3. xib相关(三) —— xib右侧标签介绍(一)
4. xib相关(四) —— 连线问题(一)
5. xib相关(五) —— 利用layout进行约束之界面(一)
6. xib相关(六) —— 利用layout进行约束之说明和注意事项(二)
7. xib相关(七) —— Storyboard中的segue (一)
8. xib相关(八) —— Size Classes(一)
9. xib相关(九) —— 几个IB修饰符(一)
10. xib相关(十) —— xib的国际化(一)
11. xib相关(十一) —— xib的高冷用法之修改视图的圆角半径、边框宽度和颜色(一)
12. xib相关(十二) —— UIStackView之基本介绍(一)
13. xib相关(十三) —— UIStackView之枚举UIStackViewDistribution使用(二)
14. xib相关(十四) —— UIStackView之UIStackViewAlignment使用(三)
15. xib相关(十五) —— UIStackView之工程实践(四)
16. xib相关(十六) —— UINib之基本介绍(一)
17. xib相关(十七) —— UINib之Introduction(二)
18. xib相关(十八) —— UINib之Nib文件(三)
19. xib相关(十九) —— UINib之Nib文件(四)
20. xib相关(二十) —— UINib之字符串资源(五)
21. xib相关(二十一) —— UINib之图像、声音和视频资源(六)
22. xib相关(二十二) —— UINib之数据资源文件(七)
23. xib相关(二十三) —— 几个xib使用场景示例(一)
24. xib相关(二十四) —— 使用IBSegueAction提升Storyboard Segues(一)
源码
1. Swift
首先看工程组织结构

接着看sb中的内容

下面就是源码了
1. Note.swift
import Foundation
class Note {
var title = ""
var body = ""
init(title: String = "", body: String = "") {
self.title = title
self.body = body
}
}
2. NotesRepository.swift
import Foundation
class NotesRepository {
static let sharedInstance = NotesRepository()
private var notes = [
Note(title: "Songs by Adam Sandler", body: """
7 Foot Man
A Christmas Song
Assistant Principal's Big Day
At A Medium Pace
Bad Boyfriend
Billy Madison's Victory Song
Buddy
Cordurory Blues
Crazy Love
Dancin' And Pantsin'
Dip Doodle
Do It For Your Mama
Fatty Mcgee
Food Innuendo Guy
Forgetful Lucy
Four Years Old
Girl
Grow Old With You
I Ran Over The Taco Bell Dog
I'm So Wasted
Joining The Cult
Listenin' To The Radio
Lunchlady Land
Memory Lane
Mother's Day Song
Moyda
Mr. Bake—o
Mr. Spindel's Phone Call
My Little Chicken
Ode To My Car
Oh Mom
Operaman
Pickin' Daisies
Red Hooded Sweatshirt
Right Field
Santa Song
Somebody Kill Me
Steve Polychronopolous
Sweat Beatrice
Teenage Love On The Phone
Thanksgiving Song
The Adventures Of The Cow
The Beating Of A High School Bus Driver
The Beating Of A High School Janitor
The Beating Of A High School Science Teacher
The Beating Of A High School Spanish Teacher
The Buffoon And The Dean Of Admissions
The Buffoon And The Valedictorian
The Chanuka Song
The Chanukah Song (ii)
The Cheerleader
The Excited Southerner Orders A Meal
The Excited Southerner Proposes To A Woman
The Goat Song
The Hypnotist
The Lonesome Kicker
The Longest Pee
The New Chanukah Song
The Respect Chant
The Thanksgiving Song
Toll Booth Willie
Voodoo
Welcome My Son
What The Hell Happened To Me?
Zittly Van Zittles
"""),
Note(title: "Quotes From The Princess Bride", body: """
“When I was your age, television was called books.”
“My name is Inigo Montoya, you killed my father, prepare to die!”
“Inconceivable!" "You keep using that word. I do not think it means what you think it means.”
“We’ll never survive!” “Nonsense. You’re only saying that because no one ever has.”
“As you wish...”
""")
]
subscript(index: Int) -> Note {
notes[index]
}
var count: Int {
notes.count
}
func add(note: Note) {
notes.append(note)
}
}
3. NotesListViewController.swift
import UIKit
class NotesListViewController: UIViewController {
private static let cellReuseId = "notesCell"
private let notesRepository = NotesRepository.sharedInstance
@IBOutlet weak var notesTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
notesTableView.dataSource = self
hideEmptyRows()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
notesTableView.reloadData()
}
@IBSegueAction func makeNewNoteViewController(coder: NSCoder) -> EditNoteViewController? {
let newNote = Note()
notesRepository.add(note: newNote)
return EditNoteViewController(note: newNote, title: "New Note", coder: coder)
}
@IBSegueAction func makeEditNoteViewController(coder: NSCoder) -> EditNoteViewController? {
guard let selectedRow = notesTableView.indexPathForSelectedRow?.row else {
return nil
}
let note = notesRepository[selectedRow]
return EditNoteViewController(note: note, coder: coder)
}
private func hideEmptyRows() {
//hide empty rows with a zero height footer view
notesTableView.tableFooterView = UIView(frame: .zero)
}
}
//MARK: - UITableViewDataSource
extension NotesListViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = notesTableView.dequeueReusableCell(withIdentifier: Self.cellReuseId)!
let note = notesRepository[indexPath.row]
cell.textLabel?.text = note.title
cell.detailTextLabel?.text = note.body
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notesRepository.count
}
}
4. EditNoteViewController.swift
import UIKit
class EditNoteViewController: UIViewController {
private let note: Note
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var bodyTextView: UITextView!
required init?(coder: NSCoder) {
fatalError("init(coder:) is not implemented")
}
init?(note: Note, title: String = "Edit Note", coder: NSCoder) {
self.note = note
super.init(coder: coder)
self.title = title
}
override func viewDidLoad() {
super.viewDidLoad()
titleTextField.text = note.title
bodyTextView.text = note.body
}
override func viewWillDisappear(_ animated: Bool) {
note.title = titleTextField.text ?? ""
note.body = bodyTextView.text ?? ""
super.viewWillDisappear(animated)
}
@IBAction func setBodyAsFirstResponder(_ sender: Any) {
bodyTextView.becomeFirstResponder()
putCursorAtBeginningOfDocument(textInput: bodyTextView)
}
private func putCursorAtBeginningOfDocument(textInput: UITextInput) {
let beginning = textInput.beginningOfDocument
textInput.selectedTextRange = textInput.textRange(from: beginning, to: beginning)
}
}
后记
本篇主要讲述了使用
IBSegueAction
提升Storyboard Segues
,感兴趣的给个赞或者关注~~~

网友评论