小编rai*_*ger的帖子

如何在Objective C中发送superview消息

我是Objective C和Cocoa的新手.我只是不知道如何发送UIView的超级视图.我无法让它发挥作用.这是我到目前为止所尝试的:

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

- (void) resetDrawType {
   self.drawType = foo;
}
Run Code Online (Sandbox Code Playgroud)

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

mySubView *mySubView = [[mySubView alloc] initWithFrame:CGRectMake(foo, foo, foo, foo)];
[self addSubview:mySubView];
[mySubView release];
Run Code Online (Sandbox Code Playgroud)

然后当子视图完成其绘图时,我想将消息resetDrawType发送到其superview,即MainView.

我试过这个

 [(MainView*)[self superview] resetDrawType];
Run Code Online (Sandbox Code Playgroud)

 [(MainView*)self.superview resetDrawType];
Run Code Online (Sandbox Code Playgroud)

......什么都行不通.我了解了非正式协议,因此我将此代码添加到MainView.h中

 @interface NSObject ( resetters )
    - (void) resetDrawType;
 @end
Run Code Online (Sandbox Code Playgroud)

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

 if ([self.superview respondsToSelector:@selector(resetDrawType:)])
    [self.superview performSelector:@selector(resetDrawType) withObject:nil];
Run Code Online (Sandbox Code Playgroud)

它也没用.我究竟做错了什么?谢谢你的帮助.

cocoa-touch objective-c

10
推荐指数
2
解决办法
1万
查看次数

标签 统计

cocoa-touch ×1

objective-c ×1