gpt4 book ai didi

ios - 如何以编程方式从 UITableViewController 顺利过渡到 UIViewController

转载 作者:行者123 更新时间:2023-12-03 18:33:08 25 4
gpt4 key购买 nike

我没有使用 Storyboard,而是以编程方式进行所有操作。

所以我的问题是,当尝试在我的手机上运行该应用程序时(当我在 Xcode 中使用模拟器时不会发生这种情况),以及当我使用以下方法时...

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let destinationVC = ThirdViewController()
destinationVC.selectedExercise = exercises![indexPath.row]

self.navigationController?.pushViewController(destinationVC, animated: true)
}

...从 UITableView 过渡到 UIView,View 的内容正常过渡,但在过渡到 UIView 之前背景会卡住一秒钟。

是什么原因造成的,我该如何解决?

这是我要转换到的 viewController 的代码。

import UIKit
import RealmSwift

class ThirdViewController: UIViewController, UITextViewDelegate {

let realm = try! Realm()

var stats : Results<WeightSetsReps>?

var weightTextField = UITextField()
var weightLabel = UILabel()

var notesTextView = UITextView()

var repsTextField = UITextField()
var repsLabel = UILabel()

var timerImage = UIImageView()

var nextSet = UIButton()
var nextExcersise = UIButton()

var selectedExercise : Exercises? {
didSet{
loadWsr()
}
}


//MARK: - ViewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()

notesTextView.delegate = self

timeClock()
navConAcc()
labelConfig()
setTextFieldConstraints()
setImageViewConstraints()
setTextViewConstraints()
setButtonConstraints()

let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)
}


//MARK: - UILabel
func labelConfig(){
weightTextField.placeholder = "Total weight..."
weightTextField.layer.borderWidth = 1
weightTextField.backgroundColor = .white
weightTextField.layer.cornerRadius = 25
weightTextField.layer.borderColor = UIColor.lightGray.cgColor

weightLabel.text = " Weight (lbs): "
weightLabel.textColor = .black

weightTextField.leftView = weightLabel
weightTextField.leftViewMode = .always


repsTextField.placeholder = "Number of Reps..."
repsTextField.layer.borderWidth = 1
repsTextField.backgroundColor = .white
repsTextField.layer.cornerRadius = 25
repsTextField.layer.borderColor = UIColor.lightGray.cgColor


repsLabel.text = " Repetitions: "
repsLabel.textColor = .black

notesTextView.layer.borderWidth = 1
notesTextView.backgroundColor = .white
notesTextView.layer.cornerRadius = 25
notesTextView.layer.borderColor = UIColor.lightGray.cgColor
notesTextView.text = " Notes..."
notesTextView.textColor = UIColor.lightGray
notesTextView.returnKeyType = .done


repsTextField.leftView = repsLabel
repsTextField.leftViewMode = .always

nextSet.layer.borderWidth = 1
nextSet.backgroundColor = .white
nextSet.layer.cornerRadius = 25
nextSet.layer.borderColor = UIColor.lightGray.cgColor
nextSet.setTitle("Next Set", for: .normal)
nextSet.setTitleColor(.black, for: .normal)
nextSet.addTarget(self, action: #selector(addNewSet), for: .touchUpInside)

nextExcersise.layer.borderWidth = 1
nextExcersise.backgroundColor = .white
nextExcersise.layer.cornerRadius = 25
nextExcersise.layer.borderColor = UIColor.lightGray.cgColor
nextExcersise.setTitle("Next Exercise", for: .normal)
nextExcersise.setTitleColor(.black, for: .normal)

[weightTextField, repsTextField, notesTextView, nextSet, nextExcersise].forEach{view.addSubview($0)}
}


//MARK: - TextView Delegates
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.text == " Notes..." {
textView.text = ""
textView.textColor = UIColor.black
}
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
}
return true
}

func textViewDidEndEditing(_ textView: UITextView) {
if textView.text == ""{
notesTextView.text = " Notes..."
notesTextView.layer.borderColor = UIColor.lightGray.cgColor
}
}

//MARK: - Dismiss Keyboard Function
@objc func dismissKeyboard(){
view.endEditing(true)
}


//MARK: - TextField Constraints
func setTextFieldConstraints(){
weightTextField.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor,padding: .init(top: 20, left: 40, bottom: 0, right: -40), size: .init(width: 0, height: 50))
repsTextField.anchor(top: weightTextField.bottomAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor, padding: .init(top: 30, left: 40, bottom: 0, right: -40) ,size: .init(width: 0, height: 50))
}


//MARK: - UIButton Functions
@objc func addNewSet(){
print("It Works")
}

//MARK: - UIButton Constraints
func setButtonConstraints(){
nextSet.anchor(top: nil, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: nil, padding: .init(top: 0, left: 40, bottom: 0, right: -150), size: .init(width: 120, height: 70))
nextExcersise.anchor(top: nil, leading: nextSet.trailingAnchor, bottom: nextSet.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 0, left: 85, bottom: 0, right: -40), size: .init(width: 120, height: 70))
}


//MARK: - ImageView Constraints
func setImageViewConstraints(){
timerImage.anchor(top: repsTextField.bottomAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor, padding: .init(top: 40, left: 0, bottom: 0, right: 0), size: .init(width: 100, height: 100))
}

//MARK: - TextView Constraints
func setTextViewConstraints(){
notesTextView.anchor(top: timerImage.bottomAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor, padding: .init(top: 40, left: 40, bottom: 0, right: -40), size: .init(width: 100, height: 110))
}


//MARK: - Navigation Bar Setup
func navConAcc(){
navigationItem.title = selectedExercise?.exerciseName
navigationController?.navigationBar.prefersLargeTitles = true
}

//MARK: - Stopwatch
func timeClock(){
let image1 = UIImage(named: "stopwatch")
timerImage = UIImageView(image: image1)
timerImage.contentMode = .scaleAspectFit

self.view.addSubview(timerImage)
}

//MARK: - Load Data
func loadWsr() {
stats = selectedExercise?.wsr.sorted(byKeyPath: "sets", ascending: true)
}

//MARK: - Save Data
func save(wsr : WeightSetsReps){
do {
try realm.write {
realm.add(wsr)
}
} catch {
print("Error saving wsr data \(error)")
}
}

}

extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero){

translatesAutoresizingMaskIntoConstraints = false

if let top = top {
topAnchor.constraint(equalTo: top, constant: padding.top).isActive = true
}

if let leading = leading {
leadingAnchor.constraint(equalTo: leading, constant: padding.left).isActive = true
}

if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: padding.bottom).isActive = true
}

if let trailing = trailing {
trailingAnchor.constraint(equalTo: trailing, constant: padding.right).isActive = true
}

if size.width != 0 {
widthAnchor.constraint(equalToConstant: size.width).isActive = true
}

if size.height != 0 {
heightAnchor.constraint(equalToConstant: size.height).isActive = true
}
}
}

enter image description here

最佳答案

您没有正确实现编程 View Controller 。以编程方式创建的 View Controller 在 loadView() 中完成其所有 View 构建,而不是 viewDidLoad()。因此,在 loadView() 中添加 View Controller 的所有 subview (调用 super.loadView())。然后使用viewDidLoad()(with调用super.viewDidLoad())做post-view工作,比如添加定时器,通知观察者等. 我怀疑滞后是由错误配置的生命周期引起的。

看来您对 iOS 或 Swift 开发还比较陌生,因此我强烈建议您不要使用扩展,尤其是在用于自动布局的 UIView 上。在开始扩展之前先了解它是如何工作的。程序化自动布局的过程是:

// adjust parameters first, like color, delegate, etc.
someView.translatesAutoresizingMaskIntoConstraints = false // set resizing to false before adding as a subview
view.addSubview(someView) // add as a subview
someView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true // add constraints

关于ios - 如何以编程方式从 UITableViewController 顺利过渡到 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59699036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com