Objective-c – Dynamically create a UIButton within a custom view – Objective C

dynamicobjective cuibuttonuiview

I am creating a custom view that will act like a footer bar so I can pass in several views rather than using IB to add etc saving time and to learn more coding.

My class first creates the background, then I add a UIButton to this, but for some reason the text is not showing and also the click is not firing.

Custom class h file:

    @interface menuViewone : UIView {

       IBOutlet UIView *menuback;

    }

    -(void)nextPage;

    -(void)menubackground;
    -(void)nextButton;

My custom class m file:

    -(void)nextPage{
        NSLog(@"Next Page Clicked");
    }


    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
                }
        return self;
    }

    -(void)menubackground{

        NSLog(@"Menu Background Loaded");
                menuback = [[UIView alloc] initWithFrame:CGRectMake(0, 410, 320, 50)];
        [menuback setBackgroundColor:[UIColor yellowColor]];
        [self addSubview:menuback];
        [menuback release];

         [self nextButton];
    }

    -(void)nextButton{

        NSLog(@"Create Next Button");

        UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        nextBtn.frame = CGRectMake(0, 410, 60, 50);
        [nextBtn setBackgroundColor:[UIColor redColor]];
        nextBtn.titleLabel.text = @"next";
        nextBtn.titleLabel.textColor = [UIColor blackColor];
        [nextBtn setTitle:@"Next" forState:UIControlStateNormal];
        [nextBtn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:nextBtn]; 

    }

I am calling it in my main m file using:

    #import "menuViewone.h"

    .......

    - (void)viewDidLoad
    {

       menuViewone *menu = [[menuViewone alloc] init];
       [menu menubackground];
       [self.view addSubview:menu];
       [menu release];

        [super viewDidLoad];


    }

So at the moment I get a yellow footer background with a red button to the left, but no action or label

1: whats going on?
2: am I doing this right?

UPDATE
I have solved the issue of the text not showing which was when I was releasing the button which would auto release as mentioned below.

So now my only issue is that the click event is not working, and I do not get my NSLog output from the -(void)nextPage{.. method.

Here is the test one I created:

   UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

  [nextBtn setFrame:CGRectMake(0, 100, 80,40)];

  [nextBtn setTitle:@"Button Title" forState:UIControlStateNormal];

  [nextBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];

  [nextBtn addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];

  [self addSubview:nextBtn];

but it seems as if the
[nextBtn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];

is not firing?

ANOTHER UPDATE:

I have tried the following piece of code on the root/ my view Controller.

viewController.h
– (void)nextPage: (id)sender;

viewController.m

 -(void)nextPage: (id)sender
  {
     NSLog(@"Next Page Clicked");
  }
  - (void)viewDidLoad
  {

       CGRect buttonFrame = CGRectMake( 10, 280, 100, 30 );
      UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
      [button setTitle: @"My Button" forState: UIControlStateNormal];
      [button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
      [self.view addSubview: button];
     [button addTarget: self 
           action: @selector(nextPage:) 
 forControlEvents: UIControlEventTouchDown];

 .....

And when I tried this the clicked method worked.

So it must be something to do with my custom class view and the setting of my button as if its not recognised it??

Thanks Si

Best Answer

NextPage does not get called because you use the incorrect signature for the selector. in:

    [nextBtn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];

the colon after nextPage means "a method called nextPage with a single argument. Something like nextPage:(int)pageNumber {} where you have nextPage {}. If you change that line to:

    [nextBtn addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];

Your action will be called.

The label I'm less sure of but it is probably not displaying because of the combination of button style and state. You should definitely replace UIButtonTypeCustom with UIButtonTypeRoundedRect. If that doesn't work make sure that the state of the button matches the state you set text for. You can either set the state of the button or, set the text for more states (with the latter being preferable).

Related Topic