2009-06-26 21 views
12

एक UIViewController के viewDidLoad विधि में, मैं यह कर:UIButton शीर्षक क्यों प्रदर्शित नहीं करता है?

UIButton *b = [[UIButton buttonWithType:UIButtonTypeRoundedRect] 
             initWithFrame:CGRectMake(0, 0, 100, 100)]; 

[b setTitle:@"Testing" forState:UIControlStateNormal]; 
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; 
[self.view addSubview:b];      // EDIT: should be 'b' 
NSLog(@"button title: %@", [b titleLabel].text); 

बटन प्रदर्शित करता है, लेकिन शीर्षक नहीं करता है। एनएसएलओजी लाइन कंसोल को "परीक्षण" प्रिंट करती है। मैं क्या गलत कर रहा हूँ पर कोई सुझाव?

+0

दो विचार: * यदि आप अपना बटन बड़ा करते हैं तो क्या होता है? * क्या आप दृश्य में कुछ और कर रहे हैं DidLoad जो बटन को प्रभावित कर सकता है? – JonathonW

+0

कृपया नीचे दिये गयेरख के समाधान को देखें क्योंकि यह उपरोक्त उपयोग किए गए डबल प्रारंभकर्ता में एक त्रुटि का वर्णन करता है और साथ ही इसके लिए फिक्स्ड का वर्णन करता है, जो सुविधा प्रारंभकर्ता [... प्रकार के साथ बटन ...] पूर्ण होने के बाद फ्रेम को अलग से असाइन करना है। बाकी ओपी का अपना समाधान सही है और वही रहता है। – eGanges

उत्तर

38

कारण है कि यह काम नहीं करता है मैं आपको बता नहीं सकता, लेकिन मैं एक समाधान है:

UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;   
b. frame = CGRectMake(0, 0, 100, 100); 

[b setTitle:@"Testing" forState:UIControlStateNormal]; 
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; 
[self addSubview:b]; 

अलग आवंटन और बटन के init से फ्रेम बनाने।

+0

मैं सहमत हूं। मैं दो शुरुआती आयकों को एक साथ जोड़कर शर्त लगाता हूं जैसे आप कर रहे हैं कुछ मुद्दों का कारण बन रहा है। बटन विथ टाइप टाइप करें और फिर फ्रेम सेट करें। – LucasTizma

+1

setTitle ने किया, धन्यवाद – Eugene

+0

हाँ, 'बटनविथ टाइप' एक स्थैतिक फैक्ट्री विधि है जो पूरी तरह प्रारंभिक बटन लौटाती है। आप पहले से शुरू किए गए बटन पर 'initWithFrame' को कॉल नहीं कर सकते हैं (परिणाम सबसे अच्छा अपरिभाषित है)। 'init' विधियों को केवल 'alloc' के बाद बुलाया जाना है। – devios1

4

इस बजाय कार्य करें:

UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
b.frame = CGRectMake(0, 0, 100, 100); 
[b setTitle:@"Testing" forState:UIControlStateNormal]; 
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; 
[self.view addSubview:b]; 
22

समस्या से

UIButton *b = [[UIButton buttonWithType:UIButtonTypeRoundedRect] 
             initWithFrame:CGRectMake(0, 0, 100, 100)]; 

buttonWithType रिटर्न एक autoreleased प्रारंभ वस्तु निहित है। आप इसे एक initWithFrame फिर से नहीं भेज सकते क्योंकि एक ऑब्जेक्ट केवल एक बार आरंभ किया जा सकता है।

अलग से इसकी अवधि सेट करें:

b.frame = CGRectMake(0, 0, 100, 100); 
0

बात आप [[UIButton alloc] initWithFrame:] या [UIButton buttonWithType:], आप दोनों एक साथ उपयोग नहीं करना चाहिए के लिए जाने के लिए है।

0

आप निम्न कर सकते हैं:

UIButton *b=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 

b=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[b setTitle:@"Button Title Here" forState:UIControlStateNormal]; 
[b setTitleColor:[UIColor blackColor] forState: UIControlStateNormal]; 
[self.view addSubview:b]; 
0

मैं एक ही समस्या थी और यह सच है कि मैं backgroundImage के बजाय छवि की स्थापना की गई थी जा रहा समाप्त हो गया। यह सब ठीक काम किया जब मैंने किया:

[button setBackgroundImage:image forState:UIControlStateNormal]; 
संबंधित मुद्दे