1

我已经用谷歌搜索了自己的盲点,无法找到我要找的东西。我是 XCode 和 Objective-C 的新手,但进展很快……但是……

如果我在 singleView 应用程序上使用 Storyboards,并且我手动将 UIViews 添加到该 Storyboard(例如,表示我要拖放的图像),我如何将这些 UIViews 放入一个数组中以便我可以以编程方式循环它们?现在,我已经能够通过代码创建子视图,然后将它们添加到数组中就好了,但是如果我首先在故事板中创建子视图(这简化了整体布局的设计),然后我尝试添加那些故事板 UIView到一个数组,该数组出现空,X 和 Y 位置为 0。

我的第一个假设是 Storyboard 中创建的 UIView 的实例在 ViewDidLoad 执行时没有实例化(我试图填充数组),但我不确定。我的目标是能够从 Storyboard 布局中直观地将这些 UIViews 放置在它们各自的位置,然后在应用程序启动期间将这些 UIViews 添加到一个数组中,将它们的起始位置保存在屏幕上等。

//

好的,感谢 Andrew 对 IBOutletCollections 的引用,我终于在 App 启动时将我的 UIViews 存储在一个数组中,但是它们表示的帧位置在控制台中被 NSLog'ing 到 {0,0},即使它们在故事板上的其他位置. 我从 ViewDidLoad 调用了一个 Init 方法,如下所示:

- (void)initialize {

    dragViews = [NSArray arrayWithObjects: dragView1, dragView2, dragView3, nil];
    dragTargets = [NSArray arrayWithObjects: dragTarget1, dragTarget2, dragTarget3, nil];

    NSLog(@"arrayImages contains %d items. Contents = %@",[dragViews count], dragViews);
    NSLog(@"arrayTarget contains %d items. Contents = %@",[dragTargets count], dragTargets);

    int i;
    int count = [dragViews count];
    for (i = 0; i < count; i++) {
        UIView *currentView = [dragViews objectAtIndex:i];
        NSLog (@"My view's center is: %@\n", NSStringFromCGRect(currentView.frame));
    }
}

但是我的控制台没有正确地表示他们当前的位置:

2013-02-15 10:43:09.538 dragAssign[4869:11303] arrayImages contains 3 items. Contents = (
    "<UIImageView: 0x92066b0; frame = (0 0; 0 0); autoresize = TM+BM; layer = <CALayer: 0x9206710>>",
    "<UIImageView: 0x9006510; frame = (0 0; 0 0); autoresize = TM+BM; layer = <CALayer: 0x9006570>>",
    "<UIImageView: 0x90065a0; frame = (0 0; 0 0); autoresize = TM+BM; layer = <CALayer: 0x9006600>>"
)
2013-02-15 10:43:09.545 dragAssign[4869:11303] arrayTarget contains 3 items. Contents = (
    "<UIView: 0x92062a0; frame = (0 0; 0 0); autoresize = TM+BM; layer = <CALayer: 0x9206350>>",
    "<UIView: 0x92055d0; frame = (0 0; 0 0); autoresize = TM+BM; layer = <CALayer: 0x92053a0>>",
    "<UIView: 0x9206650; frame = (0 0; 0 0); autoresize = TM+BM; layer = <CALayer: 0x92057d0>>"
)
2013-02-15 10:43:09.546 dragAssign[4869:11303] My view's center is: {{0, 0}, {0, 0}}
2013-02-15 10:43:09.546 dragAssign[4869:11303] My view's center is: {{0, 0}, {0, 0}}
2013-02-15 10:43:09.547 dragAssign[4869:11303] My view's center is: {{0, 0}, {0, 0}}

建议?我将如何通过该数组进行交互并获得正确的帧位置?再次感谢安德鲁为我指明了正确的方向!

4

1 回答 1

1

您应该可以使用IBOutletCollection来做到这一点。这里有一个简短的帖子解释它:http: //www.bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection/

至于保存视图的起始位置,您需要自己通过迭代出口集合数组中的视图来完成此操作-viewDidLoad,并将每个视图的框架存储在某处。

于 2013-02-14T17:17:17.013 回答