Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chhoda

macrumors 6502
Original poster
Oct 25, 2008
285
1
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:mad:"Ok" forState:UIControlStateNormal];


why is not the button displaying ?
 
Last edited by a moderator:
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:mad:"Ok" forState:UIControlStateNormal];
[self addSubview:btn];
}
return self;
}

still it is not displaying

regards
CH
 
Last edited by a moderator:
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 section of the iPhone Application Programming Guide that all iPhone developers should have read all of.
 
!

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:mad:"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:mad:"XXX" otherButtonTitles:mad:"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
 
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:mad:"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
 
yes

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 .... ... .......
 
I call this custom dialog like

// UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:mad:"XXX" otherButtonTitles:mad:"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?
 
got it

bit strange, but when i did like btn.frame = CGRectMake(0,0, 20, 20);

it worked..
no other changes in the above code...
 
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.
 
Be sure to retain when using static helper method

I struggled for a couple hours on this one...

Code:
	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 struggled for a couple hours on this one...

Code:
	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?
 
maybe..

maybe you forgot to set the color of the button's text...


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

i had that problem.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.