【问题标题】:How Do I Load or Update a View From NSObject Class After Getting the Data?获取数据后如何从 NSObject 类加载或更新视图?
【发布时间】: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


【解决方案1】:

改变

UIButton *button = [[UIButton alloc] init]; 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

【讨论】:

  • 这一次解决了我类似的问题。从你的代码我看不出有什么问题。能否请您添加“animateButton”功能的内容?
  • 这只是一个简单的 CGAffineTransform 动画来创建脉冲效果。但是,我想我已经找到了错误的产生位置。请查看我更新的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多