【发布时间】:2014-06-16 19:34:04
【问题描述】:
我在名为 HeartrateGraph 的 UIView 中制作了一个包含数据的图表。在一个名为 HRGraphInfo 的 UIViewController 中,我有一个连接的标签,它应该在触摸图形时输出值。问题是,我不知道如何使用委托从 UIView 向 UIViewController 发送触摸事件。
这是我在 UIView 中的触摸分配代码:
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
for (int i = 0; i < kNumberOfPoints; i++)
{
if (CGRectContainsPoint(touchAreas[i], point))
{
graphInfoRF.heartRateGraphString = [NSString stringWithFormat:@"Heart Rate reading #%d at %@ bpm",i+1, dataArray[i]];
graphInfoRF.touched = YES;
break;
}
}
这段代码在 touchesBegan 中,并在对象 graphInfoRF 中正确存储了数据值和数字(我只是没有显示 dataArray、kNumberOfPoints 等的声明)。
我可以使用以下方法访问 UIViewController 中的 graphInfoRF:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (graphInfoRF.touched == YES) {
self.heartRateLabel.text = graphInfoRF.heartRateGraphString;
}
else {
self.heartRateLabel.text = @"No data got over to this file";}
}
标签将显示正确的字符串,但仅在触摸图表上的数据点并且紧随其后触摸标签之后。如何更改 touchesBegan 以便在我触摸图表上的数据点后,它会自动用数据填充标签,而无需再次单独触摸标签?
【问题讨论】:
标签: objective-c xcode uiview uiviewcontroller touchesbegan