Ios 从UIView委托隐藏UIViewController中的按钮

Ios 从UIView委托隐藏UIViewController中的按钮,ios,objective-c,uiview,uiviewcontroller,delegates,Ios,Objective C,Uiview,Uiviewcontroller,Delegates,我有一个UIView类,它作为子视图加载到UIViewController中,用于捕获触摸并将其绘制到屏幕上(一个简单的绘图应用程序)。我想在绘图开始时隐藏UIViewController中的一些按钮。在UIViewController(DrawingController.m)的viewDidLoad方法中: //init the draw screen drawScreen=[[MyLineDrawingView alloc]initWithFrame:self.view.bounds]; [

我有一个UIView类,它作为子视图加载到UIViewController中,用于捕获触摸并将其绘制到屏幕上(一个简单的绘图应用程序)。我想在绘图开始时隐藏UIViewController中的一些按钮。在UIViewController(DrawingController.m)的viewDidLoad方法中:

//init the draw screen
drawScreen=[[MyLineDrawingView alloc]initWithFrame:self.view.bounds];
[drawScreen setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:drawScreen];
我在UIViewController(DrawingController.m)中有一个方法,它隐藏了UIViewController中存在的颜色选择器、笔刷大小按钮等。从同一个类(DrawingController.m)调用hide方法时效果良好。基本上,当在UIView类(MyLineDrawingView.m)中调用-(void)touchesStart:(NSSet*)toucheEvent:(UIEvent*)事件方法时,我希望隐藏按钮:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    DrawingController *drawController = [[DrawingController alloc] init];
    [drawController hideDrawIcons];

}
我已经在UIViewController中的hideDrawIcons方法中添加了一些日志记录,并且正在调用它,但是我所有的隐藏代码都不起作用:

self.undoBtn.hidden = YES;
我怀疑这是因为我正在使用DrawingController*drawController=[[DrawingController alloc]init];-但我不知道如何做不同的事情

当然,我已经在DrawingController.h头文件中公开了正确的方法,以便能够从UIView类(MyLineDrawingView.m)调用它:


希望所有这些都有意义,提前谢谢

是的,您正在视图中创建视图控制器的新副本,这是错误的。您需要的是对实际视图控制器的引用。以下是您如何做到这一点:

在MyLineDrawingView中,声明一个属性:

@property (nonatomic, weak) DrawingController *drawingController;
请注意,它被声明为弱,这将防止出现错误

实例化视图时:

drawScreen=[[MyLineDrawingView alloc]initWithFrame:self.view.bounds];
drawScreen.drawingController = self; // pass the actual view controller instance to the drawScreen object.
最后,在MyLineDrawingView中:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     [self.drawingController hideDrawIcons];
}

你说得对。您不需要创建DrawingController的新实例。 您只需要在视图类中拥有DrawingController的指针。如果您想更多地了解这项技术,可以阅读有关委托模式的内容。如果没有,下面是简单的步骤

在MyLineDrawingView.h文件中添加协议(也可以为其创建单独的文件)

最后一点零钱。为委托调用hideDrawIcons方法(DrawingController)


为了清楚起见,显然我在处理绘图等的touchesbeent方法中省略了一些代码,除了在触摸时隐藏UIViewController中的按钮(当然,在触摸结束时,我会再次显示这些按钮)之外,一切都很好。非常感谢您的回复,有几个错误。我得到一个关于line drawScreen的错误。drawingController=self;这表示“ARC不允许将Objective-C指针隐式转换为int*”。我还得到了line@property(非原子,弱)DrawingController*DrawingController的一个错误;错误为未知类型名称“DrawingController”和“属性为“弱”的属性必须为对象类型您需要在
MyLineDrawingView.h
中包含DrawingController的标题。没有这些,MyLineDrawingView就不会“知道”DrawingController。在您进行更改后,第二个错误也应该消失。实际上,我在MyLineDrawingView中包含了标题。您引用的错误表示
DrawingController
是未知类型。如果我尝试只包含DrawingController.h(#导入“DrawingController.h”-在将其包含在.m文件中之前),则包含DrawingController.h的方式一定有问题在MyLineDrawingView.h中,当我在DrawingController.h的标头内声明MyLineDrawingView时,我在该行中遇到以下错误-抱歉之前我应该包含此代码:@interface DrawingController:UIViewController{MyLineDrawingView*drawScreen;},错误为未知类型名称MyLineDrawingView-非常感谢您的回复!我在drawScreen上得到一个错误。delegate=self;行,它表示在MyLineDrawingView*类型的对象上找不到属性委托-我认为在DrawingController中有一件事可能会有所不同。我设置了以下变量:@interface DrawingController:UIViewController{MyLineDrawingView*drawScreen;}很可能您没有添加@Property(非原子,弱)id代表;这条线位于MyLineDrawingView.h中。我很抱歉。我将编辑我的回答的格式非常感谢,在我将这一行添加到MyLineDrawingView.h之后,这个解决方案起到了作用
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     [self.drawingController hideDrawIcons];
}
@protocol MyLineDrawingProtocol <NSObject>

-(void)hideDrawIcons;

@end
@interface DrawingController <MyLineDrawingProtocol>
{
    // members ....
}

@end
@interface MyLineDrawingView
{
    // members ....
}
@property (nonatomic,weak) id <MyLineDrawingProtocol> delegate;

@end
//init the draw screen
drawScreen=[[MyLineDrawingView alloc]initWithFrame:self.view.bounds];
drawScreen.delegate = self;// self is a pointer of DrawingController
[drawScreen setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:drawScreen];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ( [self.delegate respondsToSelector:@selector(hideDrawIcons)] )
        [self.delegate hideDrawIcons];

}