2010-02-26 5 views
25

UIView डॉक्स से:UIView पदानुक्रम सूचकांक कैसे प्राप्त करें? (यानी अन्य subviews के बीच में गहराई)

(void)insertSubview:(UIView *)view atIndex:(NSInteger)index 

यह बहुत अच्छा है कि मैं एक निश्चित सूचकांक पर एक UIView सम्मिलित कर सकते हैं, लेकिन मैं क्या सूचकांक एक दिया UIView है पढ़ने के लिए एक रास्ता नहीं मिल रहा है ।

मैं चाहे UIView शीर्ष पर है, या पीछे की जांच करने की आवश्यकता ...

संपादित करें: अब जब कि ब्रैंडन subviews सरणी में आदेश ने बताया, मैं एक साथ एक UIView श्रेणी मुफ्त डाउनलोड के लिए उपलब्ध कराने के डाल http://www.touch-code-magazine.com/uiview-layout-hierarchy-uiview-category-to-download/

उत्तर

48

मैं लगभग 100% यकीन है कि सूचकांक superViews subviews संपत्ति के अंदर subview के सूचकांक के रूप में ही है हूँ: कुछ काम के तरीकों subviews पदानुक्रम यहाँ संभालने के लिए।

UIView * superView = .... some view 
UIView * subView = .... some other view 
[superView insertSubview:subView atIndex:index]; 
int viewIndex = [[superView subviews] indexOfObject:subView]; 
// viewIndex and index should be the same 

मैं सिर्फ निम्न कोड के साथ इस परीक्षण किया है और यह काम करता है

UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
UIView* view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
[self.view insertSubview:view1 atIndex:1]; 
[self.view insertSubview:view2 atIndex:2]; 
[self.view insertSubview:view3 atIndex:3]; 

NSLog(@"%d", [[self.view subviews] indexOfObject:view1]); // Is 1 
NSLog(@"%d", [[self.view subviews] indexOfObject:view2]); // Is 2 
NSLog(@"%d", [[self.view subviews] indexOfObject:view3]); // Is 3 

[self.view bringSubviewToFront:view1]; 
NSLog(@"%d", [[self.view subviews] indexOfObject:view1]); // Is end of array 

[self.view sendSubviewToBack:view1]; 
NSLog(@"%d", [[self.view subviews] indexOfObject:view1]); // Is 0 
+1

मैं प्रश्न पोस्ट करने से पहले यह कोशिश कर रहा था। सबव्यूज़ सरणी में इंडेक्स वही रहते हैं :( –

+0

वास्तव में, मेरा परीक्षण दिखाता है कि सबव्यूव सरणी में इंडेक्स 'लाने के बाद कॉल के बाद बदलता है' दृश्यव्यू टॉफ्रंट: ' –

+0

आप बिल्कुल सही ब्रैंडन हैं, मेरे पास एक व्यू मैपर है जो' इंडेक्स को बदलना नहीं; दूसरी तरफ सबव्यूज़ सरणी उन्हें बदलती है। धन्यवाद। हालांकि मुझे यह स्वीकार करना चाहिए कि दस्तावेज़ कैसे इंडेक्स प्राप्त करने के बारे में अस्पष्ट हैं और क्या छोटे सूचकांक शीर्ष पर हैं या इसके विपरीत। –

7

एक पुनरावर्ती विधि सेट करें इस प्रकार है:

[self printViewHierarchy:self.view depth:0]; 
:

- (void)printViewHierarchy:(UIView *)viewNode depth:(NSUInteger)depth 
{ 
    for (UIView *v in viewNode.subviews) 
    { 
     NSLog(@"%@%@", [@"" stringByPaddingToLength:depth withString:@"|-" startingAtIndex:0], [v description]); 
     if ([v.subviews count]) 
      [self printViewHierarchy:v depth:(depth + 2)]; // + 2 to make the output look correct with the stringPadding 
    } 
} 

फिर से कॉल करने की

आउटपुट:

[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: <UILayoutContainerView: 0x622ce70; frame = (0 0; 768 1004); autoresize = W+H; layer = <CALayer: 0x622cec0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-<UINavigationTransitionView: 0x622cfa0; frame = (0 0; 768 1004); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x622cff0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-<UIViewControllerWrapperView: 0x601c4c0; frame = (0 0; 768 1004); autoresize = RM+BM; layer = <CALayer: 0x607c5f0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-<UIView: 0x6069c40; frame = (0 0; 768 1004); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x607b460>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-<UIToolbar: 0x623a550; frame = (0 0; 768 44); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x623a610>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-<UIToolbarTextButton: 0x626d6a0; frame = (7 0; 52 44); opaque = NO; layer = <CALayer: 0x626e200>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIPushButton: 0x626d920; frame = (0 7; 52 30); opaque = NO; layer = <CALayer: 0x626d9c0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-<UIView: 0x623a440; frame = (69 -1; 755 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x623a470>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIButton: 0x621cec0; frame = (-185 0; 505 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x621cc20>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x62206d0; frame = (-35 -28; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x621cc50>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x607ec40; frame = (0 7; 30 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x607ec70>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIButtonLabel: 0x62306c0; frame = (32 14; 38 16); text = 'About'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6230780>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIButton: 0x6230a00; frame = (-109 0; 510 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x6230660>> 

[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x6230a90; frame = (-35 -28; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x6230ac0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x607ebc0; frame = (0 7; 30 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x607ebf0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIButtonLabel: 0x6231620; frame = (35 14; 30 16); text = 'Staff'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6231e20>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIButton: 0x6231ea0; frame = (13 0; 465 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x6231e50>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x6231f30; frame = (-35 -28; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x6231f60>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x607eb40; frame = (0 7; 30 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x607eb70>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIButtonLabel: 0x6233430; frame = (35 14; 71 16); text = 'Procedures'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x62334a0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIButton: 0x6233520; frame = (110 0; 485 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x62334d0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x62335b0; frame = (-35 -28; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x62335e0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x607eac0; frame = (0 7; 30 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x607eaf0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIButtonLabel: 0x6234190; frame = (35 14; 52 16); text = 'Specials'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x62349d0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIButton: 0x6237560; frame = (200 0; 490 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x6237510>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x62375f0; frame = (-35 -28; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x6237620>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x607ea20; frame = (0 7; 30 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x607ea50>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIButtonLabel: 0x6238170; frame = (35 14; 46 16); text = 'Gallery'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x62389d0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIButton: 0x6234a50; frame = (305 0; 475 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x6234a00>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x6234ae0; frame = (-35 -28; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x6234b10>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x607e940; frame = (0 7; 30 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x607e970>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIButtonLabel: 0x6235670; frame = (35 14; 64 16); text = 'Education'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6235ed0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIButton: 0x6235f30; frame = (435 0; 455 45); opaque = NO; autoresize = W+BM; layer = <CALayer: 0x6235f00>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x6235fc0; frame = (-35 -28; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x6235ff0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x607e7b0; frame = (0 7; 30 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x607e7e0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIButtonLabel: 0x6236b80; frame = (35 14; 83 16); text = 'Appointment'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6236bf0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-<UIView: 0x623a370; frame = (0 0; 768 1004); hidden = YES; opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x623a3a0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-<UIView: 0x6239f40; frame = (234 427; 300 150); autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6239f70>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UILabel: 0x6239fa0; frame = (0 20; 300 22); text = 'Loading...'; clipsToBounds = YES; opaque = NO; autoresize = W+BM; userInteractionEnabled = NO; layer = <CALayer: 0x623a010>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UILabel: 0x623a120; frame = (0 40; 300 35); text = 'This may take a few minut...'; clipsToBounds = YES; opaque = NO; autoresize = W+BM; userInteractionEnabled = NO; layer = <CALayer: 0x623a190>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIActivityIndicatorView: 0x6238a70; frame = (132 93; 37 37); opaque = NO; autoresize = LM+RM+TM+BM; animations = { contents=<CAKeyframeAnimation: 0x605f810>; }; layer = <CALayer: 0x6238ad0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-<UIView: 0x62307e0; frame = (54 68; 660 916); hidden = YES; autoresize = LM+RM+H; layer = <CALayer: 0x6230810>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-<UIImageView: 0x6230840; frame = (0 0; 660 916); alpha = 0.75; hidden = YES; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x6230870>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-<UIPageControl: 0x621b7b0; frame = (0 887; 660 36); opaque = NO; autoresize = W+TM; layer = <CALayer: 0x621b870>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIImageView: 0x621ed20; frame = (279 15; 6 6); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x62215a0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIImageView: 0x621a620; frame = (295 15; 6 6); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x621b5c0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIImageView: 0x622d210; frame = (311 15; 6 6); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x621ca40>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIImageView: 0x621ca70; frame = (327 15; 6 6); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x621caa0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIImageView: 0x621cad0; frame = (343 15; 6 6); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x621cb00>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIImageView: 0x621cb30; frame = (359 15; 6 6); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x621cb60>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIImageView: 0x6226910; frame = (375 15; 6 6); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x621ed50>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-<UIScrollView: 0x623e9a0; frame = (0 0; 660 896); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x623c520>; contentOffset: {0, 0}> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-<UIView: 0x6248070; frame = (0 0; 660 896); autoresize = W+H; layer = <CALayer: 0x62480a0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UILabel: 0x62480d0; frame = (0 0; 660 35); text = 'About Us'; clipsToBounds = YES; opaque = NO; autoresize = W+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6248140>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UIImageView: 0x621f280; frame = (0 50; 660 200); hidden = YES; autoresize = W+BM; userInteractionEnabled = NO; layer = <CALayer: 0x623bc70>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-<UITextView: 0x621db30; frame = (20 35; 620 646); text = ''; clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x6237ce0>; contentOffset: {0, 0}> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-|-<UITextSelectionView: 0x6232840; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x62328e0>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-|-<UIImageView: 0x6012950; frame = (615 266; 5 124); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x6069b90>> 
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|-|-<UIWebDocumentView: 0x6a65e00; frame = (0 0; 620 50); text = ''; opaque = NO; userInteractionEnabled = NO; layer = <UIWebLayer: 0x6236980>> 
+1

या (अनियंत्रित, लेकिन काम करता है): एनएसएलओजी (@ "% @", [self.view रिकर्सिव डिस्क्रिप्शन]); –

+1

@MarkBeaton 'UIView' के लिए कोई दृश्य @interface 'चयनकर्ता' रिकर्सिव डिस्क्रिप्शन 'घोषित करता है, मुझे यह त्रुटि करने की कोशिश करने में यह त्रुटि मिली। – aleclarson

+1

यह एक निजी विधि है - मैं आमतौर पर केवल डीबगर में इसका उपयोग करता हूं (उदाहरण के लिए '' 'po [someView रिकर्सिव डिस्क्रिप्शन]' ''। कोड में, आप '' '[कुछ दृश्य प्रदर्शन चयनकर्ता: @ चयनकर्ता (रिकर्सिव डिस्क्रिप्शन)]' ' 'कंपाइलर त्रुटि से बचने के लिए, या बस (अस्थायी रूप से) उस कंपाइलर त्रुटि को अक्षम करें। –

संबंधित मुद्दे