gpt4 book ai didi

ios - 子类 UIViewControllers 的通用类?

转载 作者:行者123 更新时间:2023-12-01 19:39:39 24 4
gpt4 key购买 nike

我在一些重复太多的遗留代码中有一个展开的转义。我正在努力减少它。

- (IBAction)unwindWithNewData:(UIStoryboardSegue *)segue {
if ([segue.sourceViewController isKindOfClass:DistantVC1.class]) {
DistantVC1 *sourceVC = (DistantVC1 *)segue.sourceViewController;
self.activeUUID = sourceVC.data.uuid;

} else if ([segue.sourceViewController isKindOfClass:DistantVC2.class]) {
DistantVC2 *sourceVC = (DistantVC2 *)segue.sourceViewController;
self.activeUUID = sourceVC.data.uuid;

} else if ([segue.sourceViewController isKindOfClass:DistantVC3.class]) {
DistantVC3 *sourceVC = (DistantVC3 *)segue.sourceViewController;
self.activeUUID = sourceVC.data.uuid;

} else if ([segue.sourceViewController isKindOfClass:DistantVC4.class]) {
DistantVC4 *sourceVC = (DistantVC4 *)segue.sourceViewController;
self.activeUUID = sourceVC.data.uuid;
}
}

我想在函数顶部存储任何 View Controller 的通用版本,然后用适当的 View Controller 填充它,然后调用 self.activeUUID setter 一次。就像是:
- (IBAction)unwindWithNewData:(UIStoryboardSegue *)segue {
id sourceVC;
if ([segue.sourceViewController isKindOfClass:DistantVC1.class]) {
sourceVC = (DistantVC1 *)segue.sourceViewController;

} else if ([segue.sourceViewController isKindOfClass:DistantVC2.class]) {
sourceVC = (DistantVC2 *)segue.sourceViewController;

} else if ([segue.sourceViewController isKindOfClass:DistantVC3.class]) {
sourceVC = (DistantVC3 *)segue.sourceViewController;

} else if ([segue.sourceViewController isKindOfClass:DistantVC4.class]) {
sourceVC = (DistantVC4 *)segue.sourceViewController;
}
self.activeUUID = sourceVC.data.uuid;
}


有任何想法吗?谢谢!

最佳答案

是的,根据评论使用协议(protocol)。

  @protocol ActiveIdProvider
@property NSString *activeUUID
@end


@interface DistantVC1 : UIViewController <ActiveIdProvider> //repeat this block for each DistantVC class you have
@property NSString *activeUUID
@end
- (IBAction)unwindWithNewData:(UIStoryboardSegue *)segue {
ActiveIdProvider *sourceVC;
if ([segue.sourceViewController isKindOfClass:DistantVC1.class]) {
sourceVC = (DistantVC1 *)segue.sourceViewController;

} else if ([segue.sourceViewController isKindOfClass:DistantVC2.class]) {
sourceVC = (DistantVC2 *)segue.sourceViewController;

} else if ([segue.sourceViewController isKindOfClass:DistantVC3.class]) {
sourceVC = (DistantVC3 *)segue.sourceViewController;

} else if ([segue.sourceViewController isKindOfClass:DistantVC4.class]) {
sourceVC = (DistantVC4 *)segue.sourceViewController;
}
self.activeUUID = sourceVC.data.uuid;
}

关于ios - 子类 UIViewControllers 的通用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55910204/

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