I am trying to add an action to a button programmatically from within a custom class. However, I keep getting an error when I perform the action. I've read a lot about how to do this but am clearly making a mistake somewhere and can't figure out where.
The button is created in the first instance by dragging it onto the storyboard. I then control drag to the ViewControler.h file to get this:
@property (strong, nonatomic) IBOutlet UIButton *testButtonForClass;
In ViewControler.m, I do this:
- (void)viewDidLoad
{
[super viewDidLoad];
testClass *myClass = [[testClass alloc]init];
myClass.myButton = self.testButtonForClass;
[myClass assignActionTargets];
}
Below is the custom class Header and Implementation file.
Header File
#import <Foundation/Foundation.h>
@interface testClass : NSObject
@property (nonatomic, strong) UIButton *myButton;
-(void)assignActionTargets;
@end
Implementation File
#import "testClass.h"
@implementation testClass
-(void)assignActionTargets{
[self.myButton addTarget:
self action:@selector(myButtonInnerTap)
forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)myButtonInnerTap{
UIAlertView *a = [[UIAlertView alloc]initWithTitle:nil
message:@"testClass says hello"
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil, nil];
[a show];
}
@end