i tired to write my subview programmatically, cause i wanted to have nice rounded Button with Images. So i wrote myView.h and myView.m with an init method wich looks sth like this:
- (id)init
{
if (self = [super init]){
redBlack = [UIImage imageNamed:@"redB.png"];
redWhite = [UIImage imageNamed:@"redW.png"];
redButton = [UIButton buttonWithType:UIButtonTypeCustom];
redButton.frame = CGRectMake(20.0, 30.0, 130.0, 130.0);
[redButton setBackgroundImage:redBlack forState:UIControlStateNormal];
[redButton setBackgroundImage:redWhite forState:UIControlEventTouchDown];
[redButton addTarget:self action:@selector(controller.colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[redButton setTag:0];
[self.view addSubview:redButton];
.....
The Problem is, that colorButtonPressed is a Method, which is written in my ControllerView-Singleton. I don't know if i am doing this right, but actually i wanted to keep the MVC as clean as possible. Is it a common way to do it like this and if so, the compiler doesnt let me write controller.colorButtonPressed: in @selector ?!? Or should i specify the programmat开发者_开发百科ically written view also in my controller class?
Hopefully these questions dont sound too stupid!? And thanks in advance!Instead of
[redButton addTarget:self action:@selector(controller.colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
it should be:
[redButton addTarget:controller action:@selector(colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
the target needs to be the class the method is defined in.
If the target is set to "self" then the method should be in the same class. Seems by your code that the method is in controller which means that target should be "controller" and selector should be @selector(colorButtonPressed:)
精彩评论