gpt4 book ai didi

ios - 具有多个类的 Storyboard UIViewController

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:18 25 4
gpt4 key购买 nike

我希望 Storyboard中的一个 UIViewController 由多个类实例化。所以我在 Storyboard 中让字段“类”为空(默认情况下,它的 UIViewController?)。然后我用“MyGenericView”填充 Storyboard ID。

这是一些类:

@interface ClassA: UIViewController

@interface ClassB: UIViewController

MyGenericView 包含我在 ClassA 和 ClassB 中构建 View 所需的所有原型(prototype)。下面是我如何实例化我的 ClassA:

ClassA *myClass = (ClassA*)[storyboard instantiateViewControllerWithIdentifier:@"MyGenericView"];

最后,我的 View 显示在应用程序中,但我的 ClassA 中的代码从未被调用。 instantiateViewControllerWithIdentifier 返回的对象是一个 UIViewController,转换不起作用。

我不想用 ClassA 填充字段“class”,因为我想为 ClassB 重用此 View 。但是,我不想在 Storyboard中复制此 View 。

不知道你是否清楚,我为我糟糕的英语道歉:)

谢谢!

最佳答案

我很想找到这个问题的解决方案但唯一可行的方法(我认为)是使用委托(delegate)而不是子类化。

我的意思是,您可以实现 3 个类:ClassP、ClassC1、ClassC2;将您的 Storyboard与实现委托(delegate)协议(protocol)的 ClassP 链接起来,然后在呈现之前(进入prepearForSegue?)你可以设置委托(delegate)segue.destinationViewController(P 类型)到您的类 ClassC1 或 ClassC2 之一,显然实现了协议(protocol)。

示例代码(未测试):

P级

@protocol ClassPDelegate <NSObject>

- (void)foo;

@end

@interface ClassP : UIViewController
...
@end

C1 类和 C2 类

#import "ClassP.h"
@property (nonatomic, weak) id <ClassPDelegate> delegate;

来电类

...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"mySegue"])
{
ClassC1 *classC1 = [[ClassC1 alloc] init];
[(ClassP *)segue.destinationViewController setDelegate:classC1];
}
}
...

希望能成功!ps: 对不起,你的英语比我的好!

关于ios - 具有多个类的 Storyboard UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14962260/

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