我在從另一個類獲取CGPoint數據後更新我的UIImageView以包含按鈕的子視圖時遇到了一些問題。 在我的主要VC中,我有一個ImageView集作爲UIScrollView的子視圖,它是主ViewControllers視圖的子視圖。如何在獲取數據後從NSObject類加載或更新視圖?
在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的方法,我「M放置的按鈕:
- (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類,它將回調View Controller繪製一個子視圖。嘗試此操作時出現EXC BAD ACCESS錯誤。
但是,如果在主線程中顯式處理調用,應用程序不會崩潰,但視圖不會繪製。
我想這是當試圖將按鈕添加到圖像作爲子視圖時發生了什麼。
假設你的數據的NSArray包含有效的值,移動placeButtonsFromDatabaseWithCoordinates方法的調用你的viewWillAppear中。 – MDB983