Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically create a UIButton within a custom view - Objective C
    text
    copied!<p>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.</p> <p>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.</p> <p>Custom class h file:</p> <pre><code> @interface menuViewone : UIView { IBOutlet UIView *menuback; } -(void)nextPage; -(void)menubackground; -(void)nextButton; </code></pre> <p>My custom class m file:</p> <pre><code> -(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]; } </code></pre> <p>I am calling it in my main m file using:</p> <pre><code> #import "menuViewone.h" ....... - (void)viewDidLoad { menuViewone *menu = [[menuViewone alloc] init]; [menu menubackground]; [self.view addSubview:menu]; [menu release]; [super viewDidLoad]; } </code></pre> <p>So at the moment I get a yellow footer background with a red button to the left, but no action or label</p> <p>1: whats going on? 2: am I doing this right?</p> <p><strong>UPDATE</strong> 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. </p> <p>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.</p> <p>Here is the test one I created:</p> <pre><code> 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]; </code></pre> <p>but it seems as if the [nextBtn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];</p> <p>is not firing?</p> <p><strong>ANOTHER UPDATE:</strong></p> <p>I have tried the following piece of code on the root/ my view Controller.</p> <p>viewController.h - (void)nextPage: (id)sender;</p> <p>viewController.m</p> <pre><code> -(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]; ..... </code></pre> <p>And when I tried this the clicked method worked.</p> <p>So it must be something to do with my custom class view and the setting of my button as if its not recognised it??</p> <p>Thanks Si</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload