【发布时间】:2015-06-09 14:25:34
【问题描述】:
在从另一个类获取 CGPoint 数据后,我在更新 UIImageView 以包含按钮的子视图时遇到了一些麻烦。 在我的主 VC 中,我将 ImageView 设置为 UIScrollView 的子视图,它是主 ViewControllers 视图的子视图。
在 viewDidLoad 中,我从 NSObject 类调用一个方法来从我的数据库中获取所有按钮位置的数据:
- (void)parseCoordinateData:(NSArray*)data {
NSLog(@"Processing Data");
mapVC = [[ViewController alloc]init];
for(NSArray *table in data) {
NSLog(@"Table: %@", table);
float xValue = [[NSString stringWithFormat:@"%@", [table[0]objectForKey:@"LocationX"]] floatValue];
float yValue = [[NSString stringWithFormat:@"%@", [table[0]objectForKey:@"LocationY"]] floatValue];
NSLog(@"Coordinates: (%f,%f)", xValue, yValue);
CGPoint buttonCoordinate = CGPointMake(xValue, yValue);
[mapVC placeButtonsFromDatabaseWithCoordinates:buttonCoordinate];
}
}
在我放置按钮的主 VC 的方法中:
- (void)placeButtonsFromDatabaseWithCoordinates:(CGPoint)point {
NSLog(@"Placing Button at point: %@", NSStringFromCGPoint(point));
UIButton *button = [[UIButton alloc] init];
[self.imageView addSubview:button];
button.frame = (CGRect){.origin = point, .size = CGSizeMake(20.0, 20.0)};
button.backgroundColor = [UIColor greenColor];
button.layer.cornerRadius = 10;
button.clipsToBounds = YES;
button.userInteractionEnabled = YES;
[self animateButton:button];
[self.imageView bringSubviewToFront:button];
}
但是按钮没有出现在图像视图上。 知道如何让它工作吗?
编辑
我创建了一个非常小的项目来尝试复制这个问题。我创建了一个调用 NSObject 类的视图控制器,该类将回调视图控制器以绘制子视图。尝试此操作时出现 EXC BAD ACCESS 错误。
但是,如果在主线程中显式处理调用,应用程序不会崩溃,但视图不会绘制。
我认为这是尝试将按钮作为子视图添加到图像时发生的情况。
【问题讨论】:
-
假设您的数据 NSArray 包含有效值,请将 placeButtonsFromDatabaseWithCoordinates 方法的调用移至您的 viewWillAppear。
标签: ios objective-c uiscrollview uiimageview addsubview