views:

540

answers:

1

I'm doing an iPhone ap which contains a viewController class (PhotoListViewController). This viewController's view has a subview which contains a button attached as a subview. I'm trying to use the addTarget:action:forControlEvent method to call an instance method of the PhotoListViewController class in order to perform an action when the button is pressed but the method is not being called. Any thoughts on how to make this work?

Here's the pertinent code. I know your first though will be why am I nesting subviews, but this just a distilled version of my program - there are reasons I'm doing the nesting. I know there are probably other ways of setting up the program so I don't have to do this nesting, but my real question is - why does this not work?

/Code/

    @implementation PhotoListViewController


- (void)loadView {
    [super viewDidLoad];
 //load elements into the viewController

 //first create the frame
 CGRect frameParent = CGRectMake(0, 0, 0, 0); 
 UIView *viewPerson = [[UIView alloc] initWithFrame:frameParent];

 CGRect frameChild = CGRectMake(0, 20, 0, 0); 
 UIView *newView = [[UIView alloc] initWithFrame:frameChild];

        UIButton *btnShow = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
 btnShow.frame = CGRectMake(230, 120, 80, 30);
 [btnShow setTitle:@"View!" forState:UIControlStateNormal ];
 [btnShow addTarget:self 
action:@selector(viewPhoto_click:) 
forControlEvents:UIControlEventTouchUpInside];
 [newView addSubview:btnShow];
 [viewPerson addSubview:newView];
        [newView release];

 [self setView:viewPerson];
 [viewPerson release];
}


- (void)viewPhoto_click:(UIButton*)sender {
 NSLog(@"HI");
}
+1  A: 

It may not be related, but both viewPerson and newView have a width and height of 0. You may want to give these views a non-null size (the 3rd and 4th arguments of CGRectMake).

Martin Cote
+1 I think it's related.
ahmet emrah
thanks for the suggestion, Martin. I did try to add a width and height to both views and no luck. The button does show up, just the viewPhoto_click method is never touched.If I do take the newView out of the equation and just add the button right to viewPerson, things work find. It appears to be the nesting of the subviews that poses the problem.
Dave