本文介绍了从UIView代理中隐藏UIViewController中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIView class that is loaded in to a UIViewController as a subview, to capture touches and draw them to the screen (a simple drawing app). I want to hide some buttons in the UIViewController when drawing begins. In the UIViewController (DrawingController.m) viewDidLoad method:

//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)调用时,该隐藏方法工作正常。基本上当 - (void)touchesBegan:(NSSet *)触摸与Event:(UIEvent *)事件方法在UIView类(MyLineDrawingView.m)中调用我希望隐藏按钮:

I have a method in the UIViewController (DrawingController.m) that is hiding my colour picker, brush size buttons etc that are present within the UIViewController. The hide method works fine when called from the same class (DrawingController.m). Basically when the -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method is called within the UIView class (MyLineDrawingView.m) I am hoping to hide the buttons:

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

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

}

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

I've added some logging to the hideDrawIcons method within the UIViewController and it is getting called, but all of my hiding code is not working:

self.undoBtn.hidden = YES;

我怀疑这是因为我正在使用DrawingController *绘制UIViewController类的新实例drawController = [ [DrawingController alloc] init]; - 但我不知道如何做不同的事情?

I suspect this is because I am making a new instance of the UIViewController class with DrawingController *drawController = [[DrawingController alloc] init]; - but I'm not sure how to do things differently?

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

I have of course exposed the correct method in the DrawingController.h header file, to be able to call it from the UIView class (MyLineDrawingView.m):

-(void)hideDrawIcons;

希望所有这一切都有道理,谢谢提前!

Hopefully all of that makes sense, thanks in advance!

推荐答案

你是对的您不需要创建DrawingController的新实例。
所有你需要你有你的ViewController的指针在你的View类。如果您想了解更多有关此技术的信息,您可以阅读关于委托模式的信息。如果没有,这里是简单的步骤。

You are right. You do not need to make create new instance of DrawingController.All you need yo have pointer of your DrawingController in your View class. If you want to know more about this technology you can read about Delegate pattern. If not, here is simple steps.

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

Add protocol in your MyLineDrawingView.h file ( or you can create separated file for it )

@protocol MyLineDrawingProtocol <NSObject>

-(void)hideDrawIcons;

@end

在您的DrawingController.h中

In your DrawingController.h

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

@end

在您的MyLineDrawingView.h中

In your MyLineDrawingView.h

@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];

最后更改。为代理调用hideDrawIcons方法(DrawingController)

And last change. Call hideDrawIcons method for delegate ( DrawingController )

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ( [self.delegate respondsToSelector:@selector(hideDrawIcons)] )
        [self.delegate hideDrawIcons];

}

这篇关于从UIView代理中隐藏UIViewController中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:20