Objective C协议与Java接口相同? [英] Objective C protocol as an equal to Java Interface?

查看:119
本文介绍了Objective C协议与Java接口相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题不仅在于标题,而在于我将如何实现这一点,而不是试图将Java / Flash设计强制转换为Objective C(iPhone)程序。

The question is not only regarding the headline, but more of a "how will I achieve this, without trying to force a Java/Flash design into an Objective C (iPhone)program".

我有6个视图扩展了UIView,这些视图都有不同的行为,但共享某些方法,如 - (void)update - (void)changeState:(NSInteger)state

I have 6 views that extends UIView, these views all have different behavior but share certain methods, like -(void) update and -(void) changeState:(NSInteger)state.

一个viewController,它的工作是更新,实例化和显示这些视图有一个开关块来做这件事。所以switch(4){...}实例化UIView4,但因为我需要对当前实例化视图的引用(要做 update changeState: ),我的ViewController上有一个名为self.currentView的UIView属性。当实例化的UIView4扩展UIView时,我可以轻松地去 [self.currentView addSubview:UIView4instance] 然后释放UIView4instance。

A viewController, whose job is it to update, instantiate and display these views has a switch block to do this. So switch(4) {...} instantiates UIView4, but as I need a reference to the currently instantiated view (to do update and changeState:), I have a UIView property on my ViewController called self.currentView. As the instantiated UIView4 extends UIView I can easily go [self.currentView addSubview:UIView4instance] and then release the UIView4instance.

现在如何在视图上调用 [UIView4instance update] 方法?或者 [UIView5instance changeState] 等等。
因为我把它添加到 self.currentView 这是类型UIView它不再有任何理由相信它有更新 changeState:方法,这意味着我无法迭代视图并向他们发送这些消息。

Now how will I call the [UIView4instance update] method on the view? or the [UIView5instance changeState] etc. etc. Since I added it to self.currentView which is of type UIView it no longer has any reason to believe it has an update or changeState: method, meaning I cannot iterate the views and send them these messages.

这种方法带来了大量其他问题,每次我需要进行任何操作时,我都需要测试并键入我的视图在他们。

This approach brings on a ton of other problems, I would need to test and typeCast my views each time I needed to do any operations on them.

如果我在Java中使用这种复合模式方法。我要么编写一个所有视图(UIView1,UIview2 ...... UIViewN)都会实现的接口。或者也许是一个抽象类,所有视图都继承了 changeState:更新方法。

If I were doing something like this Composite Pattern approach in, say, Java. I would either write an interface that all the views (UIView1, UIview2.... UIViewN) would implement. Or maybe an abstract class that all the views inherited the changeState: and update methods from.

这样我就可以 self.currentView 只知道我在视图中添加对象,它们都符合这些方法。

This way I could have self.currentView just know that I'm adding objects to your view and they all conform to these methods.

我能想到的两个解决方案,我的Objective-C经验非常小:
使用委托或类别来做这件事,但这似乎有点过分杀戮:/
我想我也可以扩展UIView然后扩展我的类再次扩展UIView,但是可能有一个原因Objective-C不直接支持抽象类......

The two solutions I can think of, with my very small Objective-C experience is: doing it with delegation or categories, but this seems overkill in every way :/ I guess I could also extend UIView and then extend my class extending UIView again, but there is probably a reason Objective-C does not directly support abstract classes...

希望有人能指出我在这些问题上的正确方向。
谢谢:)

Hope someone could point me in the right direction regarding these issues. Thanks:)

推荐答案

是的,它是平等的。你可以声明一个协议

Yes it is equal. You can declare a protocol

@protocol MyViewProtocol
-(void)update;
-(void)changeState:(NSInteger)state;
@end

并声明您的子类

@interface MyUIView3 : UIView<MyViewProtocol> {
....
}
....
@end

然后,声明

UIView<MyViewProtocol>* view=[[MyUIView3 alloc] init];

表示 view 是一个实例(属于 UIView 的子类,它也符合 MyViewProtocol
我认为就像它在Java中的工作方式一样。请参阅协议上的Apple doc

means that view is an instance (of a subclass of) UIView which also conforms to MyViewProtocol. Just the way it works in Java, I think. See the apple doc on protocols.

这篇关于Objective C协议与Java接口相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆