2010-08-06 14 views
8

मेरे पास एक कस्टम UIView उपclass है। आईबी में, मैंने एक "प्लेसहोल्डर" UIView निर्दिष्ट किया है और कक्षा को मेरे वर्ग के नाम पर सेट किया है। DrawRect विधि का मेरा ओवरराइड काम कर रहा है, और पृष्ठभूमि ठीक से रंग दे रही है, लेकिन initWithFrame फायरिंग नहीं है। क्यूं कर?UIView Subclass initWithFrame नहीं कहा जा रहा है?

- (id)initWithFrame:(CGRect)frame { 
    NSLog(@"initWithFrame"); 
    if ((self = [super initWithFrame:frame])) { 
     noteTextFont = [[UIFont boldSystemFontOfSize:12] retain]; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    UIColor *backgroundColor = [UIColor blackColor]; 
    [backgroundColor set]; 
    CGContextFillRect(context, rect); 

    UIColor *labelColor = [UIColor lightGrayColor]; 
    [labelColor set]; 
    [notificationLabel drawInRect:CGRectMake(44, 18, rect.size.width-20, rect.size.height) withFont:noteTextFont lineBreakMode:UILineBreakModeTailTruncation]; 

    [self setNeedsLayout]; 
} 
+0

क्या आप वह कोड दिखा सकते हैं जहां आप कस्टम व्यू बनाते हैं? क्या आप कॉल कर रहे हैं: \t UIView * fred = [[UIView alloc] initWithFrame: myFrame]; – joelm

उत्तर

-2

क्योंकि आप एक निब यह की तरह लग रहा से इस दृश्य को प्रारंभ कर रहे हैं, तो यह डिफ़ॉल्ट (UIView के) initWithNibName उपयोग कर रहा है: के बजाय initWithFrame का उपयोग कर आरंभ किया जा रहा:

+0

तो क्या उप-वर्ग में प्रारंभिकरण को ओवरराइड करना असंभव है? एक और प्रश्न। आईबी में, दृश्य 50pt लंबा होना तय है, लेकिन इसे 42 फ्रेम लंबा फ्रेम के साथ शुरू किया जा रहा है। –

+0

मैं इनिट को ओवरराइड करने का प्रयास करूंगा: साथ ही देखें कि इसे कॉल किया जाता है या नहीं। मैं भूल गया कि UIView वास्तव में initWithNibName में नहीं है। यह UIViewController –

52

चीजें एक निब से लोड -(id)initWithCoder:(NSCoder*)coder साथ inited कर रहे हैं

+13

में एक विधि है, मेरी इच्छा है कि लोग वापस आएं और जवाब सही के रूप में चिह्नित करें। –

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