gpt4 book ai didi

ios - iOS 中 UIViewController 的子类化

转载 作者:行者123 更新时间:2023-12-02 01:52:29 25 4
gpt4 key购买 nike

我对 iOS 中的 UIViewController 子类化感到困惑,我有一个符合 UICollectionViewDataSource 协议(protocol)的父 View Controller (在实现文件内的私有(private)接口(interface)中)。

/* Parent.m */

@interface Parent () <UICollectionViewDataSource>

// this CollectionView is connected to storyboard
@property (weak, nonatomic) IBOutlet UICollectionView *CollectionView;

@end


@implementation Parent


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.somecount;
}

@end

然后我创建一个从父级继承的 subview Controller 。子级对 UICollectionViewDataSource 一无所知,因为数据源是在父级私有(private)接口(interface)中实现的。

/* child.h */

@interface child : parent
// nothing was mentioned here that parent has a method to set the count using 'somecount'
@end

然后我将 mainstoryboard 中的 View Controller 设置为 subview Controller 。

ios 如何从父属性“somecount”获取值并为子属性设置该值?

谢谢。

最佳答案

你问:

how come ios get the value from parent's property somecount and set the value for child?

子类始终继承其 super 类的属性和方法。它们可能是也可能不是公共(public)接口(interface)(您没有向我们展示 somecount 的声明,所以我们不知道),但无论如何,它们都在那里并且将在运行时解析(除非您覆盖 child 中的那些方法/属性,而您似乎没有这样做)。如果 parent 中有私有(private)方法和属性,则您可能在编译时从 child 中看不到,但它们仍然存在,并且在运行时会正常运行。

因此,当具有 Collection View 的场景指定 child 作为 Collection View 的数据源时,如果 child 没有实现这些 UICollectionViewDataSource 方法,它将自动最终调用 parent 的那些方法。同样,当这些方法中的任何一个引用 somecount 时,如果 child 没有覆盖它,它最终将再次调用 parent 的适当访问器方法。最重要的是,child 自动继承parent 的所有行为、方法和属性。

关于ios - iOS 中 UIViewController 的子类化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18028939/

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