触发drawRect重绘视图的按钮 [英] Button to trigger drawRect to redraw a view

查看:83
本文介绍了触发drawRect重绘视图的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有子视图的VC.在 subview级别中,我有一个按钮.我希望按钮导致视图被更新.

I have a VC with a subview. At the subview level I have a button. I want the button to result in the view being updated.

IBAction 代码在VC中.它当前所做的只是切换 BOOL 值.

The IBAction code is in the VC. All it currently does is toggle a BOOL value.

返回子视图drawRect:方法是测试 BOOL 并相应地更改视图的代码.

Back in the subview drawRect: method is the code that tests the BOOL and changes the view accordingly.

我的观察是,当按下按钮时,IBAction代码会运行,切换 BOOL ,但不会调用 drawRect:方法.

My observation is that when the button is pressed, the IBAction code runs, toggles the BOOL, but the drawRect: method doesn't get called.

我尝试手动调用drawRect(这不是很好的做法,而且行不通).我尝试调用setNeedsDisplay,但是那也不起作用(无法弄清楚如何引用以编程方式创建的子视图).

I tried calling drawRect manually (not good practice and didn't work). I tried calling setNeedsDisplay but that too didn't work (couldn't figure out how to reference the subview, which is created programmatically).

建议.

IBAction代码很简单,但包含在下面:

IBAction code is trivial but included below:

- (IBAction)rotateWindowButtonWasPressed:(id)sender
{
    // just flip the boolean here. Need to make the code change in drawRect: in top2View.m
    if ([myGlobals gWinVertical])
        [myGlobals setGWinVertical:NO];
    else
        [myGlobals setGWinVertical:YES];
    // Hoping this would force a redraw, but bounds have to change...:(
    //_top2ViewOutlet.contentMode = UIViewContentModeRedraw;
}

推荐答案

不知道发生了什么-上面有一个答案解决了我的问题,但是现在不见了?无论如何-答案是以编程方式创建按钮,然后我可以将方法包括在子视图级别的drawRect:所在的位置,并使用setNeedsDisplay重新绘制屏幕.与使用IB并在VC级别上具有IBAction代码相比.

Not sure what happened - there was an answer above that solved my issue but now it's gone? Anyway - the answer is to create the button programmatically and then I can include the method at the subview level where drawRect: is, and using setNeedsDisplay to repaint the screen. That vs. using IB and having the IBAction code at the VC level.

这篇关于触发drawRect重绘视图的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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