PDA

View Full Version : UIButton programming without using interface builder




chhoda
Dec 2, 2008, 03:47 AM
I am required to make a view where number of buttons to be displayed will depend upon the argument to init. I want to create click event handlers for them dinamically too .. how can i make it possible ? using interface builder is out of question, obviously

CH

on start i tried doing this inside my view..

UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
btn.center = self.center;
[btn setTitle:@"Ok" forState:UIControlStateNormal];


why is not the button displaying ?



robbieduncan
Dec 2, 2008, 04:05 AM
Because you've not added the button to any visible view.

chhoda
Dec 2, 2008, 04:09 AM
also tried buttonWithType ... no luck :(

Thanks,

I have done that ... i did not paste the whole code..

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
[self setBackgroundColor:[UIColor whiteColor]];
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.center = self.center;
[btn setTitle:@"Ok" forState:UIControlStateNormal];
[self addSubview:btn];
}
return self;
}

still it is not displaying

regards
CH

robbieduncan
Dec 2, 2008, 04:12 AM
also tried buttonWithType ... no luck :(

That makes no difference: you have not added the button to a visible view in a visible window.

You should ensure you have read and understood the basic documentation, in this case the Window and Views (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/WindowsandViews/chapter_4_section_1.html#//apple_ref/doc/uid/TP40007072-CH8) section of the iPhone Application Programming Guide that all iPhone developers should have read all of.

chhoda
Dec 2, 2008, 04:19 AM
Let me paste the whole code ..

___________

#import "CustomAlert.h"


@implementation CustomAlert


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
[self setBackgroundColor:[UIColor whiteColor]];
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.center = self.center;
//[btn setTitle:@"Ok" forState:UIControlStateNormal];
[self addSubview:btn];
}
return self;
}


- (void)drawRect:(CGRect)rect {
// Drawing code
}


- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {

}

- (void)dealloc {
[super dealloc];
}


@end


___________


I call this custom dialog like

// UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:@"XXX" otherButtonTitles:@"YYY", @"ZZZ", nil];
//
// [myAlert show];
CustomAlert* alert = [[CustomAlert alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
alert.userInteractionEnabled = YES;
[self.superview addSubview:alert];

the commented code is how i was doing, but i am needed to do it through custom alert

now essentially this customalert is a view which is a sub view of already visible view. And my button is a sub view under custom alert. So what's the problem ? Should not it show the button at least ?

moreover the white customalertview is displayed. but the button is not ...



CH

robbieduncan
Dec 2, 2008, 04:19 AM
Thanks,

I have done that ... i did not paste the whole code..

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
[self setBackgroundColor:[UIColor whiteColor]];
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.center = self.center;
[btn setTitle:@"Ok" forState:UIControlStateNormal];
[self addSubview:btn];
}
return self;
}

still it is not displaying

regards
CH

Is the view you are adding the button to visible? If you look at the UICatalog sample code you can see how Apple create buttons (and most other UI elements) programatically. In particular the ButtonsViewController.m file might help you

chhoda
Dec 2, 2008, 06:10 AM
Yes the buttom I am adding the view to is visible with white colour. But surprisingly enough the button is not visible..

I saw the UICatalog application also does similar thing...

- (void)setView:(UIView *)inView
{
if (view)
[view removeFromSuperview];
view = inView;
[self.view retain];
[self.contentView addSubview:inView];

[self layoutSubviews];
}

it adds the button to cell's contentview as a subview..

Help .... ... .......

dejo
Dec 2, 2008, 10:22 AM
I call this custom dialog like

// UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:@"XXX" otherButtonTitles:@"YYY", @"ZZZ", nil];
//
// [myAlert show];
CustomAlert* alert = [[CustomAlert alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
alert.userInteractionEnabled = YES;
[self.superview addSubview:alert];


In which method are you defining and add the CustomAlert? Also, why is it not a subclass of UIView? And what is self.superview at this point? Perhaps you want self.view or even window?

chhoda
Dec 3, 2008, 12:12 AM
bit strange, but when i did like btn.frame = CGRectMake(0,0, 20, 20);

it worked..
no other changes in the above code...

CommanderData
Dec 3, 2008, 04:49 AM
bit strange, but when i did like btn.frame = CGRectMake(0,0, 20, 20);

it worked..
no other changes in the above code...

Maybe the OS is too smart for our own good... a 10x10 pixel button would be extremely small on the screen and very hard to press. It's possible that the iPhone wont even render them below a certain size.

levous
Apr 6, 2009, 09:15 AM
I struggled for a couple hours on this one...


CGRect buttonRect = CGRectMake(0, 0, 200, 40); // whatever frame you need
UIButton *logInRegisterButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; // there's the retain needed
// UIButton *logInRegisterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; << this will not work because the release following adding to subview destroys it
[logInRegisterButton setFrame:buttonRect];
[logInRegisterButton setTitle:@"Log In or Register" forState:UIControlStateNormal];
[[self view] addSubview:logInRegisterButton];
[logInRegisterButton release];

dejo
Apr 6, 2009, 11:14 AM
I struggled for a couple hours on this one...
Not sure about that. I thought the addSubview: does its own retain of the passed view.

robbieduncan
Apr 6, 2009, 11:24 AM
Not sure about that. I thought the addSubview: does its own retain of the passed view.

It does.

Snarf
Apr 6, 2009, 12:42 PM
I struggled for a couple hours on this one...


CGRect buttonRect = CGRectMake(0, 0, 200, 40); // whatever frame you need
UIButton *logInRegisterButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; // there's the retain needed
// UIButton *logInRegisterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; << this will not work because the release following adding to subview destroys it
[logInRegisterButton setFrame:buttonRect];
[logInRegisterButton setTitle:@"Log In or Register" forState:UIControlStateNormal];
[[self view] addSubview:logInRegisterButton];
[logInRegisterButton release];


I'm sure someone will correct me if I'm wrong, but I think you'll have a memory leak doing it this way. I don't think you want to explicitly retain your button when you create it, since it will be autoreleased for you. Since it is being autoreleased, you do not want to explicitly release it either. This should cause your button to be dealloc'd properly. But I'm fairly new to all this, so someone can slap me if I'm wrong. ;)

Edit: On second look, it probably works, there's just an extra retain/release?

holylance
Sep 21, 2010, 09:45 AM
maybe you forgot to set the color of the button's text...


[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

i had that problem.

Luke Redpath
Sep 22, 2010, 07:58 AM
I'm pretty sure a button initialise with a type will have a 0x0 frame which is why you were not seeing it until you gave it a bigger frame.

sameza
Nov 6, 2010, 06:59 AM
hii :)

example with UILabel ...

http://www.forgeniuses.com/2008/03/12/iphone-hello-world-using-uilabel

work 100% ;)