Custom TabBar with UIButtons - uibutton

First: Sorry for my bad English!
I have a class in which I create UIButton. The UIButton I add a UIView. In the ViewController I would like to add these custom tabbar. This works well so far. However, there are problems with addTarget: action: forControlEvents: - How can I run from the ViewController an instance method in the class? The class is of type NSObject. My code:
TabBar *tabBar = [[TabBar alloc] init];
[tabBar tabBarButton];
[tabBar.home addTarget:tabBar action:#selector(switchView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tabBar.tabView];
And here is my Class:
#implementation TabBar
#synthesize tabView, home, list, charts, favorits, more;
-(void)tabBarButton{
tabView = [[UIView alloc] initWithFrame:CGRectMake(0, 360, 320,56)];
home = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 43, 56)];
list = [[UIButton alloc] initWithFrame:CGRectMake(63, 0, 40, 56)];
charts = [[UIButton alloc] initWithFrame:CGRectMake(123, 0, 49, 56)];
favorits = [[UIButton alloc] initWithFrame:CGRectMake(192, 0, 66, 56)];
more = [[UIButton alloc] initWithFrame:CGRectMake(278, 0, 42, 56)];
[home setImage:[UIImage imageNamed:#"home.png"] forState:UIControlStateNormal];
[list setImage:[UIImage imageNamed:#"liste.png"] forState:UIControlStateNormal];
[charts setImage:[UIImage imageNamed:#"charts.png"] forState:UIControlStateNormal];
[favorits setImage:[UIImage imageNamed:#"favorites.png"] forState:UIControlStateNormal];
[more setImage:[UIImage imageNamed:#"more.png"] forState:UIControlStateNormal];
home.tag = 1;
list.tag = 2;
charts.tag = 3;
favorits.tag = 4;
more.tag = 5;
[tabView addSubview:home];
[tabView addSubview:list];
[tabView addSubview:charts];
[tabView addSubview:favorits];
[tabView addSubview:more];
}
-(void)switchView{
NSLog(#"hi");
}
When I start the app, it crashes immediately when I press one button.

If you're getting an unrecognized selector error, you might try:
[tabBar.home addTarget:tabBar action:#selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
Note the colon behind action:#selector(switchView:)

Related

Objective-c : action:#selector(xxxxxx) not called

I'm currently trying to add a button to my app. If the user push on it, it should display an alert.
I want to add the button to a section header of a tableview. Here is my code in tableView:viewForHeaderInSection:
UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
And in the same class, I have the method :
-(void)showMyAlert{
NSLog(#"HERE SHOW ALERT");
...
}
The button is correctly displayed but the showMyAlert method is never called when I push on it.
Does anyone have an idea of what is wrong with my code ?
Thanks if you could help me :)
Change your UIImageView as UIView and return it as the header view.
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.
Modified below lines of code, Try like this:-
[button addTarget:self action:#selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)showMyAlert:(id)sender{
NSLog(#"HERE SHOW ALERT");
...
}
Try this, in viewDidLoad,
-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}

Button action doesn't respond when switched viewcontroller

I have an app where the viewcontroller is switched if a button is pressed. I switch like this:
NewViewController *nv = [[NewViewController alloc] init];
[nv NewButtonTapped:self];
[self.view addSubview:nv.view];
Then in my NewViewController.m:
- (void)NewButtonTapped:(id)sender
{
backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
backgroundImage.image = [UIImage imageNamed:#"cubePage.png"];
[self.view addSubview:backgroundImage];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(964, 100, 60, 150);
button.enabled = YES;
button.userInteractionEnabled = YES;
[button setTitle:#"D" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped:(id)sender
{
NSLog(#"Tapped");
}
All images and buttons are loaded and appears fine in the new viewcontroller but the button action buttonTapped: doesn't respond when I'm tapping the button. This code works fine in the first viewcontroller.What's the problem?

UIView Animation Block Not Doing Anything

I have a very small animation I want to do by moving a view's frame using blocks (rather than the animation headers).
My view hierarchy is quite big so I will explain everything in regards of the specific view I want to animate.
The biggest view is the one that takes the whole screen, so it is 320x480 points. Inside this view, I have another view called "keypadContainerView" that is 320x200 points, located on 0, 261 and it appears at the bottom of the screen. This keypadContainerView has a subview called keypadView, which has a bunch of small buttons as subviews.
Biggest View -> KeypadContainerView -> keypadView -> UIbuttons.
An screenshot:
Although this could go without saying, the keypad there is the keypadContainerView, with is't keypadView and buttons. I need this hierarchy because I'm aiming for eye candy here, and each layer has a different background.
The way I create and deal with that view is here:
keypadViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 261, 320, 200)] autorelease]; //(Original is 0, 261, 320, 200)
keypadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
[keypadViewContainer addSubview:keypadView];
//Add the buttons
[self.view addSubview:keypadViewContainer];
And this is how I'm trying to animate it:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIView animateWithDuration:5.0 animations:^{
self.keypadViewContainer.frame = CGRectMake(0, 0, 320, 200);
} completion:NULL];
}
What is funny is that if I NSLog something in the animations: parameter, I actually get some output so the method is definitely getting called. It's just not animating anything at all.
The whole controller code is here, in case you need it:
//UnlockKeyboardViewController
//------------------------------------------------------------------------
#interface UnlockKeyboardViewController : UIViewController
{
NSArray *buttons;
NSArray *passcodeFields;
UIImage *buttonBackgroundImage;
UIImage *buttonBackgroundHighlightedImage;
UIImage *middleButtonBackgroundImage;
UIImage *middleButtonBackgroundImageHighlighted;
UIImage *screenBackgroundImage;
UIImage *infoViewContainerImage;
UIImage *keypadViewContainerImage;
UIImage *passcodeFieldsContainerImage;
UIImage *infoViewImage;
UIImage *passcodeViewImage;
UIView *infoViewContainer;
UIView *keypadViewContainer;
UIView *passcodeFieldsContainer;
UIView *infoView;
UIView *keypadView;
UIView *passcodeFieldsView;
UIView *infoToDisplayView; //The view the programmer passes to show in infoView.
}
#property(nonatomic, retain)UIImage *buttonBackgroundImage;
#property(nonatomic, retain)UIImage *buttonBackgroundHighlightedImage;
#property(nonatomic, retain)UIImage *screenBackgroundImage;
#property(nonatomic, retain)UIImage *keypadViewBackgroundImage;
#property(nonatomic, retain)UIImage *infoViewContainerImage;
#property(nonatomic, retain)UIImage *keypadViewContainerImage;
#property(nonatomic, retain)UIImage *passcodeFieldsContainerImage;
#property(nonatomic, retain)UIImage *infoViewImage;
#property(nonatomic, retain)UIImage *passcodeViewImage;
#property(nonatomic, retain)UIView *infoToDisplayView;
//Properties for container views.
#property(nonatomic, retain)UIView *infoViewContainer;
#property(nonatomic, retain)UIView *keypadViewContainer;
#property(nonatomic, retain)UIView *passcodeFieldsContainer;
#end
#implementation UnlockKeyboardViewController
#synthesize buttonBackgroundImage = _buttonBackgroundImage;
#synthesize buttonBackgroundHighlightedImage = _buttonBackgroundHighlightedImage;
#synthesize screenBackgroundImage = _screenBackgroundImage;
#synthesize keypadViewBackgroundImage = _keypadViewBackgroundImage;
#synthesize infoViewContainerImage = _infoViewContainerImage;
#synthesize keypadViewContainerImage = _keypadViewContainerImage;
#synthesize passcodeFieldsContainerImage = _passcodeFieldsContainerImage;
#synthesize infoViewImage = _infoViewImage;
#synthesize passcodeViewImage = _passcodeViewImage;
#synthesize infoToDisplayView = _infoToDisplayView;
//Synthesizers for container views.
#synthesize infoViewContainer = _infoViewContainer;
#synthesize keypadViewContainer = _keypadViewContainer;
#synthesize passcodeFieldsContainer = _passcodeFieldsContainer;
-(void)loadView
{
[super loadView];
self.view.frame = CGRectMake(0, 0, 320, 480);
_unlockKeyboardBundle = [NSBundle bundleForClass:[self class]];
buttonBackgroundImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"button" ofType:#"png"]];
middleButtonBackgroundImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"middleButton" ofType:#"png"]];
buttonBackgroundHighlightedImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"pushedButton" ofType:#"png"]];
middleButtonBackgroundImageHighlighted = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"pushedButtonMiddle" ofType:#"png"]];
infoViewContainerImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"infoViewContainerImage" ofType:#"png"]];
keypadViewContainerImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"keypadViewContainerImage" ofType:#"png"]];
passcodeFieldsContainerImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"passcodeViewContainerImage" ofType:#"png"]];
infoViewImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"infobackground" ofType:#"png"]];
passcodeViewImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"passcodescreen" ofType:#"png"]];
//self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:#"lockbackground" ofType:#"png"]]];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
keypadViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 261, 320, 200)] autorelease]; //(Original is 0, 261, 320, 200)
keypadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
[keypadViewContainer addSubview:keypadView];
keypadViewContainer.backgroundColor = [UIColor colorWithPatternImage:keypadViewContainerImage];
NSMutableArray *tempButtons = [NSMutableArray array];
self.view.opaque = YES;
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setTitle:#"1" forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 0, 106, 50);
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setTitle:#"4" forState:UIControlStateNormal];
button4.frame = CGRectMake(0, 50, 106, 50);
UIButton *button7 = [UIButton buttonWithType:UIButtonTypeCustom];
[button7 setTitle:#"7" forState:UIControlStateNormal];
button7.frame = CGRectMake(0, 100, 106, 50);
UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
[cancel setTitle:NSLocalizedString(#"cancel", #"Cancel string") forState:UIControlStateNormal];
cancel.frame = CGRectMake(0, 150, 106, 50);
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setTitle:#"2" forState:UIControlStateNormal];
button2.frame = CGRectMake(106, 0, 108, 50);
UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
[button5 setTitle:#"5" forState:UIControlStateNormal];
button5.frame = CGRectMake(106, 50, 108, 50);
UIButton *button8 = [UIButton buttonWithType:UIButtonTypeCustom];
[button8 setTitle:#"8" forState:UIControlStateNormal];
button8.frame = CGRectMake(106, 100, 108, 50);
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setTitle:#"0" forState:UIControlStateNormal];
button0.frame = CGRectMake(106, 150, 108, 50);
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setTitle:#"3" forState:UIControlStateNormal];
button3.frame = CGRectMake(214, 0, 106, 50);
UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
[button6 setTitle:#"6" forState:UIControlStateNormal];
button6.frame = CGRectMake(214, 50, 106, 50);
UIButton *button9 = [UIButton buttonWithType:UIButtonTypeCustom];
[button9 setTitle:#"9" forState:UIControlStateNormal];
button9.frame = CGRectMake(214, 100, 106, 50);
UIButton *cancelOrDelete = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelOrDelete setTitle:#"<-" forState:UIControlStateNormal];
cancelOrDelete.frame = CGRectMake(214, 150, 108, 50);
[tempButtons addObject:button1];
[tempButtons addObject:button2];
[tempButtons addObject:button3];
[tempButtons addObject:button4];
[tempButtons addObject:button5];
[tempButtons addObject:button6];
[tempButtons addObject:button7];
[tempButtons addObject:button8];
[tempButtons addObject:button9];
[tempButtons addObject:button0];
[tempButtons addObject:cancel];
[tempButtons addObject:cancelOrDelete];
buttons = [[NSArray arrayWithArray:tempButtons] retain];
for(UIButton *theButton in buttons)
{
if([theButton.currentTitle isEqualToString:#"2"] || [theButton.currentTitle isEqualToString:#"5"] || [theButton.currentTitle isEqualToString:#"8"] || [theButton.currentTitle isEqualToString:#"0"])
{
[theButton setBackgroundImage:middleButtonBackgroundImage forState:UIControlStateNormal];
[theButton setBackgroundImage:middleButtonBackgroundImageHighlighted forState:UIControlStateHighlighted];
}else
{
[theButton setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
[theButton setBackgroundImage:middleButtonBackgroundImageHighlighted forState:UIControlStateHighlighted];
}
[keypadView addSubview:theButton];
}
[self.view addSubview:keypadViewContainer];
passcodeFieldsView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 110)] autorelease];
passcodeFieldsContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 151, 320, 110)] autorelease];
passcodeFieldsContainer.backgroundColor = [UIColor colorWithPatternImage:passcodeFieldsContainerImage];
passcodeFieldsView.backgroundColor = [UIColor colorWithPatternImage:passcodeViewImage];
NSMutableArray *passTemps = [NSMutableArray array];
UITextField *tf1 = [[[UITextField alloc] initWithFrame:CGRectMake(24, 27, 50, 50)] autorelease];
UITextField *tf2 = [[[UITextField alloc] initWithFrame:CGRectMake(98, 27, 50, 50)] autorelease];
UITextField *tf3 = [[[UITextField alloc] initWithFrame:CGRectMake(172, 27, 50, 50)] autorelease];
UITextField *tf4 = [[[UITextField alloc] initWithFrame:CGRectMake(246, 27, 50, 50)] autorelease];
[passTemps addObject:tf1];
[passTemps addObject:tf2];
[passTemps addObject:tf3];
[passTemps addObject:tf4];
passcodeFields = [[NSArray arrayWithArray:passTemps] retain];
for(UITextField *theTf in passcodeFields)
{
theTf.borderStyle = UITextBorderStyleBezel;
theTf.backgroundColor = [UIColor whiteColor];
theTf.enabled = NO;
theTf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
theTf.textAlignment = UITextAlignmentCenter;
theTf.adjustsFontSizeToFitWidth = YES;
theTf.alpha = 0.7;
theTf.secureTextEntry = YES;
[passcodeFieldsView addSubview:theTf];
}
[passcodeFieldsContainer addSubview:passcodeFieldsView];
[self.view addSubview:passcodeFieldsContainer];
infoView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 151)] autorelease];
infoView.backgroundColor = [UIColor colorWithPatternImage:infoViewImage];
infoViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 151)] autorelease];
infoViewContainer.backgroundColor = [UIColor colorWithPatternImage:infoViewContainerImage];
[infoViewContainer addSubview:infoView];
[self.view addSubview:infoViewContainer];
[pool drain];
}
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIView animateWithDuration:5.0 animations:^{
self.keypadViewContainer.frame = CGRectMake(0, 0, 320, 200);
} completion:NULL];
}
-(void)dealloc
{
[buttons release];
[passcodeFields release];
[buttonBackgroundImage release];
[buttonBackgroundHighlightedImage release];
[screenBackgroundImage release];
[infoView release];
[keypadView release];
[passcodeFieldsView release];
[infoViewContainerImage release];
[keypadViewContainerImage release];
[passcodeFieldsContainerImage release];
[infoViewImage release];
[passcodeViewImage release];
[infoToDisplayView release];
[super dealloc];
}
#end
I have a small feeling it may have something to do with the way I'm dealing with my views, so there's the whole source code.
Any help with this will be highly appreciated.
Instead of those :
keypadViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 261, 320, 200)] autorelease];
Use
Self.keypadViewContainer = .....
and your #synthesize are wrong.
You declare iVars explicitly without underscore,
But use synthesize to create underscored iVars.
So your properties correspond to
_keypadViewContainer
but in your loadView you assign to keypadViewController
Quickest fix: just use #synthesize instead of synthesize = ...
(that is only if you don't use Xcode 4.4!)
Best fix: get rid of the explicit iVar declaration and
Make sure to use _keypadViewContainer etc. when you access iVar directly like in dealloc.
Ideal fix: use ARC and use IB to build your keypad interface (why don't you btw?)

Adding CustomView on a NavigationBar

This is my code.
- (void)viewWillAppear:(BOOL)animated
{
customView = [[UIView alloc] init];
UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(410, -20, 30, 40)];
[btnAdd setBackgroundColor:[UIColor blackColor]];
[btnAdd setBackgroundImage:[UIImage imageNamed:#"oie_815362uTrAOBMX.png"] forState:UIControlStateNormal];
[btnAdd addTarget:nil action:#selector(addCustomer:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnAdd];
UIButton *btnJump = [[UIButton alloc] initWithFrame:CGRectMake(450, -20, 30, 40)];
[btnJump setBackgroundColor:[UIColor blueColor]];
[btnJump setBackgroundImage:[UIImage imageNamed:#"oie_8153842yVgkR6XD.jpg"] forState:UIControlStateNormal];
[btnJump addTarget:nil action:#selector(showMenue:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnJump];
UIButton *btnBarItem = [[UIButton alloc] initWithFrame:CGRectMake(300, -20, 100, 40)];
[btnBarItem setBackgroundColor:[UIColor greenColor]];
[btnBarItem setBackgroundImage:[UIImage imageNamed:#"oie_99342oliBc5Sy.jpg"] forState:UIControlStateNormal];
[btnBarItem addTarget:nil action:#selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnBarItem];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, -10, 30, 30)];
[imageView setImage:[UIImage imageNamed:#"oie_972931iW4SHiZU.jpg"]];
[customView addSubview:imageView];
UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -20, 80, 40)];
[mainLabel setText:#"Kunden"];
[mainLabel setBackgroundColor:[UIColor lightGrayColor]];
[mainLabel setTextColor:[UIColor blackColor]];
[mainLabel setFont:[UIFont fontWithName:#"Arial" size:18]];
[customView addSubview:mainLabel];
[self.navigationItem setTitleView:customView];
}
- (IBAction)showMenue:(id)sender
{
NewPopUpmenu *options = [[NewPopUpmenu alloc] initWithNibName:#"PopupMenu" bundle:nil];
[options.view setFrame:CGRectMake(720, 0, 300, 200)];
[self.view addSubview:options.view];
}
Note: Problem is that when i press on my "jump" button it doesn't access it's selector Method (showMenue:).
You are specifying a target of nil for each button's target-action pair. If your action method is implemented in the same class, your target should be self:
[btnAdd addTarget:self
action:#selector(addCustomer:)
forControlEvents:UIControlEventTouchUpInside];
// ...
// Same for other buttons...
// ...
Technically, if you specify nil for the target parameter, the responder chain is searched for an object that responds to the action. The problem you are having could possibly be down to the fact that your showeMenu: method isn't the signature that your action is expecting. The action is looking for a method signature that looks like this:
- (void)showeMenu:(id)sender;
Yet you have specified the IBAction return type in the implementation.

Button's action causes "invalid selector" crash -- why?

This code results in an "invalid selector" error when the button I create is pressed. Where is the test function fetched from?
Main.m
mainScreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
[self.view addSubview:mainScreen];
TaskButtons *tB = [[TaskButtons alloc] init];
[mainScreen addSubview:[tB TaskStart]];
TaskButtons.m
- (UIButton*)TaskStart {
CGRect buttonFrame = CGRectMake(500, 206, 400, 35);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
[button setTitle:#"Task Button" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.titleLabel.textAlignment = UITextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(test) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)test{
NSLog(#"test line");
}
It seems that the test function isn't being called. Doesn't setting the button's target to self here mean it should look in the TaskButtons class for the function called test?
The problem is ARC is releasing the instantiated object too soon. So to solve this I would need to retain it longer.
Main.h
#import "TaskButtons.m"
#interface ViewController : UIViewController {
TaskButtons *tB;
}
#property (nonatomic, retain) TaskButtons *tB;
[button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside];
- (void)test:(id)sender{
NSLog(#"test line");
}
Syntax problem :) In your code replace these lines.