I have created UIButton programatically , which is added to UIViewController which is also created programtically. I donot know what to pass as AddTarget.
I have currently passed optionView.self in addtarget
UIWindow *window1 = [[UIApplication sharedApplication] keyWindow];
optionView = [[UIViewController alloc]init];
optionView.view.frame=CGRectMake(0, 0, 960, 640);
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(0, 0, 200, 100);
[button setTitle:@"Press Me" forState:UIControlStateNormal];
[button addTarget:optionView.self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[optionView.view addSubview:button];
[window1 addSubview: optionView.view];
And also this is my selector method
-(IBAction)buttonClicked:(id)sender{
NSLog(@"This works");
}
And also i am getting following error
unrecognized selector sent to instance 0x1fd7b600′
Please not that i am using Unity3d Game Engine. This code is in (Unity3d it is in APPController.mm)
Your ViewController
optionViewdoes not have buttonClicked selector, that’s why app crashes.If you want, you can create child class from ViewController, implement your method there and create optionView as instance of your new class. Then it will respond to call.
Or another way – you can extend base ViewController class by creating category for it with
buttonClickedselector.