我已經Google'd自己失明,一直未能找到我要找的東西。我是XCode和Objective-C的新手,但進度相當快...但是...將故事板UIViews添加到陣列
如果我在單個應用程序上使用Storyboard,並且手動將UIView添加到該Storyboard(例如,表示圖像,我要拖放),我如何將這些UIViews放到一個數組中,這樣我就可以通過編程來循環它們了?現在,我已經能夠通過代碼創建子視圖,然後將它們添加到Array中,但是如果我先在Storyboard內創建子視圖(這簡化了總體佈局的設計),然後嘗試添加這些Storyboard UIViews到一個陣列,該陣列空X & Y位置爲0.
我的第一個假設是在Storyboard創建的UIViews的實例沒有在ViewDidLoad被執行時實例化(我在哪裏試圖填充陣列),但我不確定。我的目標是能夠在視覺上把這些UIViews它們的各自位置從故事板佈局,然後添加這些UIViews到在應用程序啓動的陣列,從而節省了屏幕的起始位置等
//
好的,感謝Andrew對IBOutletCollections的引用,我終於得到了我的UIViews存儲在App啓動時的數組,然而它們表示的框架位置是NSLog到控制檯{0,0},即使它們在其他地方故事板。我有一個初始化方法從viewDidLoad中被調用,看起來像下面這樣:
- (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}}
建議?我將如何通過該陣列進行交流並獲得正確的框架位置?再次感謝Andrew爲我指出正確的方向!
安德魯,真棒建議!謝謝你。我從來不知道IBOutletCollections。 – pscarnegie 2013-02-15 15:49:07