PDA

View Full Version : UIButton title will not show!???




ArtOfWarfare
Dec 8, 2009, 09:13 PM
I've been going through both the Developer Documentation on this and Googling for answers for an hour or two now, and I just can't seem to find out why this isn't working.

I have a custom class based off of UIAlertView...

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
if (self = [super initWithTitle:@"Add Assignment" message:@" \n \n \n " delegate:(id)delegate cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil])
{
// New Assignment Name
addAssignmentName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 27.0)];
addAssignmentName.borderStyle = UITextBorderStyleRoundedRect;
addAssignmentName.autocapitalizationType = UITextAutocapitalizationTypeWords;
addAssignmentName.autocorrectionType = UITextAutocorrectionTypeNo;
addAssignmentName.placeholder = @"New Assignment Name";
[self addSubview:addAssignmentName];
[addAssignmentName becomeFirstResponder];
[addAssignmentName release];

// New Assignment Class Name
addAssignmentClass = [[UIButton buttonWithType:UIButtonTypeRoundedRect] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 37.0)];
[addAssignmentClass setTitle:@"Select Class" forState:UIControlStateNormal];
[addAssignmentClass setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
addAssignmentClass.backgroundColor = [UIColor clearColor];
addAssignmentClass.titleLabel.adjustsFontSizeToFitWidth = TRUE;
[addAssignmentClass addTarget:self action:@selector(selectClass:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:addAssignmentClass];

[self setTransform:CGAffineTransformMakeTranslation(0.0, 85.0)];
}
return self;
}

The UITextField shows up just fine, but all I get for the UIButton is a rounded rectangle.

Upon further inspection, I found that changing the button type I initiate with to a UIButtonTypeCustom will properly display the title, but not the background.

So why isn't the button displaying the title when I change it to a RoundedRect type? Trying to make a rounded rect button in Interface Builder and giving it a title worked just fine; why doesn't it work programmatically? (It's easier to set the button up programmatically since it's being added to a UIAlertView.)



kainjow
Dec 8, 2009, 09:34 PM
Sorry don't have an answer, but I saw this:
addAssignmentClass = [[UIButton buttonWithType:UIButtonTypeRoundedRect] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 37.0)];

Don't do that. Instead, do this:
addAssignmentClass = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addAssignmentClass.frame = CGRectMake(12.0, 90.0, 260.0, 37.0);
buttonWithType: is already calling an initialization method, so you don't need to call it again.

ArtOfWarfare
Dec 9, 2009, 12:13 PM
Thanks!

Changing that fixed it perfectly!

The odd things that happen when you misuse initWithFrame...