Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在自定义视图中调用方法_Objective C_Ios_Xcode - Fatal编程技术网

Objective c 在自定义视图中调用方法

Objective c 在自定义视图中调用方法,objective-c,ios,xcode,Objective C,Ios,Xcode,我正在创建一个简单的绘图应用程序,其中有一个自定义视图,可以在屏幕上绘制线条。我正在从viewcontroller文件调用自定义view.m文件中的方法。我已经把它设置好了,这样它就可以自动完成我的方法,这意味着它知道它存在,但没有启动 在我的自定义视图中,BezierSigCapView.m - (void)erase { path = [UIBezierPath bezierPath]; [pointsArray removeAllObjects]; [self se

我正在创建一个简单的绘图应用程序,其中有一个自定义视图,可以在屏幕上绘制线条。我正在从viewcontroller文件调用自定义view.m文件中的方法。我已经把它设置好了,这样它就可以自动完成我的方法,这意味着它知道它存在,但没有启动

在我的自定义视图中,BezierSigCapView.m

- (void)erase {
    path = [UIBezierPath bezierPath];
    [pointsArray removeAllObjects];
    [self setNeedsDisplay];
    NSLog(@"ERASE!");
}
在my View Controller.h文件中

@property (weak, nonatomic) BezierSigCapView *myView;
在my View Controller.m文件中

/// in viewDidLoad
BezierSigCapView *theView = [[BezierSigCapView alloc] init];
self.myView = theView;

/// my button code
- (IBAction)ClearButton:(UIBarButtonItem *)sender {
    [self.myView erase];
    NSLog(@"Should Erase");
}

请检查您是否已连接
-(iAction)ClearButton:(UIBarButtonItem*)发送方

按按钮。

@hermann为我指出了正确的方向。我以编程方式在interface builder中创建视图。我删除了以编程方式创建的视图,并将一个出口作为属性连接到interface builder上的视图。我还去掉了多余的“theView”

//In view controller.h file
@property (strong, nonatomic) IBOutlet BezierSigCapView *myView;

//In view controller.m file
//in the implementation
@synthesize myView;

//I also removed the code below from the view controller.m file that was causing another issue
self.myView = [[BezierSigCapView alloc] init];

您确定要进入
clearButton
?这里没有您的代码,但是您添加了ViewController.h文件中声明的iAction原型吗?您是否已将该按钮与Interface Builder中的此操作关联?您能否解释一下为什么此BezierSigCapView*theView=[[BezierSigCapView alloc]init];self.myView=theView;为什么创建thView?如果可以在类中使用self.myView,那么iAction是在接口中声明还是仅在实现中声明并不重要。关键的问题是它是否正确连接——我对此表示怀疑。你可以通过编程方式创建“myView”。为什么在这里使用弱引用?在这种情况下,它不应该很强大吗?这与您的问题没有直接关系,但可能会导致出现下一个问题。请检查self.myView是否已初始化,然后记录它。