iOS中的Activity生命周期是什么?

dgsult0t  于 2023-08-08  发布在  iOS
关注(0)|答案(2)|浏览(136)

实际上,我想说iOS ViewControllers和Android Activities都有自己的生命周期方法。例如,ViewController.viewDidLoad()的等价物是Activity.onCreate()
否则我还需要知道另一个的等价物:

  • OnStart()
  • OnRestart()
  • OnResume()
  • OnStop()
  • OnDestroy()
  • OnPause()
kzmpq1sx

kzmpq1sx1#

这是Android与iOS生命周期的比较:


的数据

*注意:viewDidUnload在iOS 6之后不再使用

mgdq6dx1

mgdq6dx12#

以下是如何用Droid的方式完成它:

import Foundation
import SwiftUI
import UIKit

public class AppleActivity : UIViewController {
    
  
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }
    
    override public func viewDidLoad() -> Void {
        super.viewDidLoad()
        onCreate()
    }
    
    override public func viewWillAppear(_ animated: Bool) -> Void {
        super.viewWillAppear(animated)
        onStart()
    }

    
    public func initialize(){
        NotificationCenter.default.addObserver(self, selector: #selector(onStart), name: UIApplication.willEnterForegroundNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onResume), name: UIApplication.didBecomeActiveNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onPause), name: UIApplication.willResignActiveNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onStop), name: UIApplication.didEnterBackgroundNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onDestroy), name: UIApplication.willTerminateNotification, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    
    public func onCreate() {
        print("onCreate()")
    }
    
    @objc public func onStart() {
        print("onStart()")
    }
    
    @objc public func onResume() {
        print("onResume()")       
    }
    
    @objc public func onPause() {
        print("onPause()")        
    }
    
    @objc public func onStop() {
        print("onStop()")         
    }
    
    @objc public func onDestroy() {
        print("onDestroy()")       
    }
    
    
}

struct AppleActivityStruct : UIViewControllerRepresentable {
    
    typealias UIViewControllerType = AppleActivity
    
    public func makeUIViewController(context : Context) -> AppleActivity {
        return AppleActivity()
    }
    
    public func updateUIViewController(_ uiViewController : AppleActivity, context : Context) {
            // Update the view controller here if needed
    }
}

字符串
现在你得到你所有的回调触发每次你想要的,没有压力+没有wahala

N.B

由于通过NotificationCenter.default.addObserver(self, selector: #selector(onStart), name: UIApplication.willEnterForegroundNotification, object: nil)onStart()在第一次创建UIViewController的场景时可能不会被调用,这就是为什么在viewWillAppear(_ animated: Bool)中存在对它的调用

相关问题