[英]Set delegate of viewcontrollers in UITabbarController
I have the following setup in my app: 我的应用程序中具有以下设置:
A UITabbarController with 3 viewcontrollers, with embeded UINavigationControllers. 具有3个视图控制器的UITabbarController,具有嵌入式UINavigationControllers。 The 3 viewcontrollers inheret/superclass from a UIViewController subclass called "SVC", in order to implement elements which is used in all of the 3. viewcontrollers and prevent duplicated code.
UIViewController子类(称为“ SVC”)中的3个viewcontroller inheret /父类,以便实现在所有3个viewcontroller中使用的元素并防止重复代码。 In this "SVC" class I have setup a delegate called "dismissDelegate" (which is used to tell when the tabbarcontroller is dimissed).
在此“ SVC”类中,我设置了一个称为“ dismissDelegate”的委托(用于告知何时tabbarcontroller被撤消)。
@protocol ModalViewDelegate <NSObject>
- (void)didDismissModalViewFrom:(UIViewController *)viewController;
@end
@property (weak, nonatomic) id <ModalViewDelegate> dismissDelegate;
My other viewcontroller which segues to the UITabbarController, implements this delegate in order to get information about, when the tabbarcontroller is dismissed. 我的另一个与UITabbarController绑定的viewcontroller实现了该委托,以便获得有关tabbarcontroller被关闭时的信息。
the SVC class notifies the delegate of dismissal of the tabbar like so: SVC类将这样的选项卡通知委托人解雇:
[self.dismissDelegate didDismissModalViewFrom:self];
I now want to set the delegate of all the viewcontrollers which inherts from the SVC class (all the tabbar viewcontrollers) to this viewcontroller and I try to do this via the prepareToSegue method like so: 现在,我想将继承自SVC类(所有选项卡式视图控制器)的所有视图控制器的委托设置为此视图控制器,然后尝试通过prepareToSegue方法执行此操作,如下所示:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ToTab"]) {
UITabBarController *tabBarController = segue.destinationViewController;
for (UINavigationController *navController in tabBarController.viewControllers) {
for (UIViewController *vc in navController.viewControllers) {
_SubclassVC = (SVC *) vc.superclass;
_SubclassVC.dismissDelegate = self;
}
}
}
}
But I get the following error: 但是我收到以下错误:
+[SVC setDismissDelegate:]: unrecognized selector sent to class 0xbca68
My questions: 我的问题:
+[SVC setDismissDelegate:]: unrecognized selector sent to class 0xbca68
See the + 看到+
The plus sign idicates that you are calling a class method. 加号表示您正在调用类方法。 You must have tried setting a class variable by a setter.
您必须尝试通过设置器设置类变量。 But a property represents instance variables only.
但是属性仅表示实例变量。 Therefore the setters and getters that are automatically generated are intance methods only.
因此,自动生成的setter和getter仅为实例方法。 (starting with a minus - in error messages like this).
(从减号开始-在这样的错误消息中)。
And that is what you do: 那就是你要做的:
_SubclassVC = (SVC *) vc.superclass;
_SubclassVC.dismissDelegate = self;
For whatever reason (probably by mistake or misunderstanding) you take the vc instance and get its superclass
. 无论出于何种原因(可能是由于错误或误解),您都可以使用vc实例并获取其
superclass
。 vc.superclass
returns a class object, not an object (meaning not an instance, in Obj-C class objects are objects too). vc.superclass
返回类对象,而不是对象(在Obj-C类对象中,对象也不是实例)。 Then you typecast it to (SVC *)
just to stop the compiler from throwing errors (or warnings - not sure). 然后,将其强制转换为
(SVC *)
,以阻止编译器抛出错误(或警告-不确定)。
Well, I guess that you wondered yourself why you have to typecast it at all. 好吧,我想您想知道自己为什么必须进行所有类型转换。 That's the reason :)
那就是原因:)
Next, you assign self
to a property dismissDelegate
. 接下来,将
self
分配给属性dismissDelegate
。 The compiler does that because you typecasted it to SVC* which does have a property dismissDelegate
. 编译器执行此操作是因为您将其类型转换为SVC *,后者确实具有属性
dismissDelegate
。 The compiler will actually call the setter setDismissDelegate
as usual in contructs like this. 实际上,编译器将像这样在结构中像往常一样调用setter
setDismissDelegate
。
BUT at runtime the message (or selector) setDismissDelegate: is not sent to an SVC*
but to a class
object. 但是在运行时,消息(或选择器)setDismissDelegate:不会发送到
SVC*
而是发送到class
对象。 And the class SVC
does not have a method (or selector) +setDismissDelegate:
and therefore cannot resolve the message. 而且
SVC
类没有方法(或选择器) +setDismissDelegate:
,因此无法解析该消息。 And that is exactly what the error message is telling you. 这正是错误消息告诉您的内容。
All right, now we get to your questions. 好的,现在我们可以解决您的问题。 1. Well, it is not the way I would do it, but that is certainly one way of achiving it.
1.嗯,这不是我要做的方式,但这当然是实现它的一种方式。 2. If you want to stick with that unusual approach then do this minor change and you will get rid of the error:
2.如果您要坚持使用这种不寻常的方法,那么请进行此较小的更改,您将摆脱错误:
for (SVC *vc in navController.viewControllers) {
vc.dismissDelegate = self;
}
There is no point in fetching the superclass object. 获取超类对象没有任何意义。 If you cannot access the property of a superclass then you did something wrong with the inheritance chain.
如果您无法访问超类的属性,则您对继承链做错了。 If you want to be on the save side:
如果您想成为保存方:
for (UIViewController *vc in navController.viewControllers) {
if (vc isKindOfClass:[SVC class]){ //BTW, this is good usage of the class object
SVC *svc = (SVC*) vc;
svc.dismissDelegate = self;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.