【问题标题】:How to call UIView method from another ViewController in iOS如何从 iOS 中的另一个 ViewController 调用 UIView 方法
【发布时间】:2023-03-08 03:55:01
【问题描述】:

我已经从

实现了手绘教程
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/. 

我需要在我的视图控制器中调用它的- (void)drawBitmap;- (void)drawBitmapGreen; 方法来更改按钮操作的颜色。我做了类似下面的事情。

#import <UIKit/UIKit.h>
#import "checklistController.h"
@interface SmoothedBIView : UIView

@property(nonatomic,assign) checklistController *trnsferColor;
- (void)drawBitmapGreen;
@end

@implementation SmoothedBIView

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self drawBitmapred];
    [self drawBitmapGreen];
    [self setNeedsDisplay];
    [path removeAllPoints];
    ctr = 0;
}
- (void)drawBitmapGreen
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    if (!incrementalImage) // first time; paint background white
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
        [[UIColor clearColor] setFill];
        [rectpath fill];
    }
    [incrementalImage drawAtPoint:CGPointZero];
    [[UIColor greenColor] setStroke];
    [path stroke];
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //SmoothedBIView.trnsferColor = self;
}

...

 @interface checklistController :   UIViewController

 @property(nonatomic,strong) IBOutlet UIView *vPaintPreview; //SmoothedBIview outlet for hidden and unhidden property.
 @property(nonatomic,assign) SmoothedBIview *SBIView;
-(IBAction)editvImage;
-(IBAction)RedSketch;
-(IBAction)GreenSketch;

更新代码

@implementation file

-(IBAction)GreenSketch
{
    SmoothedBIView *SBIview = [[SmoothedBIView alloc] init];
    [SBIview drawBitmapGreen];
}

我在这里做错了什么?

【问题讨论】:

  • 查看一个视图,将您的类分配给它,使其属性,然后使用该属性调用其方法
  • 你为什么这样使用 (SmoothedBIView *SBIview = self;)?
  • @ML:我在一个网页链接中看到了这种方法。是的,这是错误的并且导致代码崩溃:(
  • 我已更新按钮操作代码。但这并没有给我想要的结果。
  • 这是错误的 OOP 设计。你可以挑鼻子,也可以挑朋友。但是你不能挖你朋友的鼻子。在另一个控制器的视图上调用方法会耦合您的代码并增加维护成本。

标签: iphone ios objective-c uiview uiviewcontroller


【解决方案1】:

您是否尝试过以下方法:

-(IBAction)GreenSketch
{
    SmoothedBIView *SBview = [[SmoothedBIView alloc] init];
   [SBview drawBitmapGreen];
   [SBview setNeedsDisplay];

 }

【讨论】:

  • 查看我的 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)事件代码。这里需要一些调整...
  • 既然已经使用了touch end方法,为什么还要使用action方法?
  • 当你点击按钮时,setneedsdisplay会在这个action方法中被调用,此时不会调用touchended..
  • 我在 ViewController 中为不同的颜色放置了不同的按钮,并在 UIView 类中为这些颜色提供了方法。但是在红色按钮操作 drawBitmapRed 以及 touchesEnded 方法被调用并产生绿色。我需要换题吗?
猜你喜欢
  • 1970-01-01
  • 2012-05-10
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多