ios - 在继承中我们可以通过 viewDidLoad

标签 ios iphone inheritance viewdidload

我有一个 UIViewController_A。 我这个 View Controller 的 vidDidLoad 我有一个获取一些数据的 http 请求。

我继承了UIViewController_A,创建了UIViewController_B。 现在我可以绕过 [super viewdDidLoad];在 UIViewController_B 中。

因为如果我调用 [super viewdDidLoad];在 xxx_B 然后它正在从 xxx_A 进行 http 调用,我不想要这个。

最佳答案

viewDidLoad() 方法应该总是调用 super.viewDidLoad()。不这样做可能会导致 Controller 无法正确实例化。

相反,您可以覆盖父 Controller 的所需行为。例如:

class ParentController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        performOperation()
    }

    func performOperation() {
        print("Calling WebService from ParentController")
    }
}

class ChildController: ParentController {
    override func viewDidLoad() {
        super.viewDidLoad() // <--- ALWAYS REQUIRED
        // Custom initialization
    }

    override func performOperation() {
        print("Overriding parent operation so it doesn't call the WebService")
    }
}

关于ios - 在继承中我们可以通过 viewDidLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515311/

相关文章:

ios - Swift:NSUserDefaults 没有立即设置

iphone - 默认 iPhone 数字键盘

java - 扩展 JFrame 总是一个坏主意吗?

c++ - 编写子类构造函数的实现

inheritance - 如何从 ViewController 调用方法?

ios - 使用 CAKeyFrameAnimation 的时变绘图的高效动画

iOS UIView 渲染到 UIImage - Apple 的示例代码只工作一次

java - 如何在 Java 中加密,在 Android 和 iOS 中解密

iphone - 如何比较 NSDates 以查找数组中最早和最晚的日期?

ios - Swift 中的 FireBase 电话号码验证错误