如何在Objective C中消息superview [英] How to message superview in Objective C

查看:117
本文介绍了如何在Objective C中消息superview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective C和Cocoa的新手。我只是不知道如何消息的UIView的superview。我不能得到它的工作。这是我尝试到目前为止:

I am new to Objective C and Cocoa. I just don't get it how to message the superview of an UIView. I can't get it work. Here is what i tried so far:

在我的MainView我有一个名为resetDrawType的方法:

In my MainView i have a method named resetDrawType:

- (void) resetDrawType {
   self.drawType = foo;
}

同样在MainView中,我创建一个子视图并将其添加到MainView: p>

Also in the MainView i create a subview and add it to MainView:

mySubView *mySubView = [[mySubView alloc] initWithFrame:CGRectMake(foo, foo, foo, foo)];
[self addSubview:mySubView];
[mySubView release];

然后当子视图完成它的绘图我想发送消息resetDrawType到它的superview, MainView。

Then when the subview finished its drawing i want to send the message resetDrawType to its superview, which is the MainView.

我试过这个

 [(MainView*)[self superview] resetDrawType];

 [(MainView*)self.superview resetDrawType];

...什么没有工作。我学习了非正式协议,所以我添加这段代码到MainView.h

…what didn't work. I learned about Informal Protocols so i added this code to MainView.h

 @interface NSObject ( resetters )
    - (void) resetDrawType;
 @end

但仍然没有。
接下来我发现这个选择器的东西,并尝试这个在子视图:

But still nothing. Next i found out about this selector thing and tried this in the subview:

 if ([self.superview respondsToSelector:@selector(resetDrawType:)])
    [self.superview performSelector:@selector(resetDrawType) withObject:nil];

它也没有工作。我做错了什么?感谢您的帮助。

It also didn't work. What am I doing wrong? Thanks for your help.

推荐答案

您不应该有一个子视图告诉其superview什么。您应该将superview作为视图类的代理。

You shouldn't have a subviews tell its superview much of anything. You should make the superview a delegate of your view class.

// MainView
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(...) delegate:self];

然后将委托保存到 id delegate 。完成绘制后:

Then save the delegate to an instance variable declared by id delegate. When you finish drawing:

// MyView
- (void)doDrawing {
  drawStuff();
  [delegate didFinishDrawingView:(MyView *)self]
}

在你的MainView中,实现这个方法

Now back in your MainView, implement this method

- (void)didFinishDrawingView:(MyView *)aView;

来做你想要的。

所有这一切的要点是,你的应用程序边缘的小类(如一个小子视图)不应该需要知道它们上面的大类如何工作。这个设置允许视图在链上沟通,但是带有传达其自身状态的消息,而不是指示其他对象做特定事情的消息。这是Cocoa的结构,以便类可以很容易地重用和他们的事件可以改变用途,但你需要他们是。

The point of all this is so that the small classes at the fringe of your app (like a small subview) shouldn't need to know how the large classes above them work. This setup allows the view to communicate up the chain, but with a message that conveys its own status instead of a message that instructs other object to do something specific. This is the way Cocoa is structured so that classes can be easily reused and their events can be repurposed however you need them to be.

你的superview应该知道该怎么做其子视图完成。

Your superview should know what to do when its subview finishes. The subview should just let people know its finished.

在子视图类的标题中声明一个实例变量,如下所示:

Declare an instance variable in your subview class's header like this:

id delegate;

为您的子视图类编写一个启动器,如下所示:

Write an initiailizer for your subview class that looks like this:

- (id)initWithFrame:(CGRect)frame delegate:(id)aDelegate {
  [super initWithFrame:frame];
  delegate = aDelegate;
  return self;
}

现在你有一个初始化器,它将接受一个委托,保存该委托,允许您随后调用其上的方法。

Now you have an initializer that will accept a delegate, saves that delegate, and allows you to then call methods on it.

这篇关于如何在Objective C中消息superview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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