Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have view controller and a custom object that will return a view with buttons. I to set the button's target to a method on the view controller. I just want this object to create the view i want and then return it for me to addSubview:.

CreateView.m

//create button
[button addTarget:(view controller) action:@selector(buttonMethod) forControlEvents:UIControlEventTouchUpInside];

How can i do this? Pass the view controller's self to the method?

share|improve this question

2 Answers

up vote 1 down vote accepted

so CreateView is basically a factory responsible for creating views? then yes, pass the pointer to the viewController to the method.

e.g.

@interface CreateView : NSObject {
}
+ (UIView*)createViewFor:(id)target;
@end
share|improve this answer
Yes, it would just be a factory. I don't see any clear advantage in the view class with delegates over doing it this way. Thank you. – Log139 Jan 28 at 15:52
I wasn't implying it was bad :) -- please accept – Daij-Djan Jan 28 at 16:30

Please Try this. It may help you for solve this issue.

[button addTarget:self action:@selector(buttonMethod:) forControlEvents:UIControlEventTouchUpInside];

-(void)buttonMethod:(id)sender
{
  CreateView *c=[CreateView alloc]initWithNibName:@"CreateView" bundle:nil];
  [self.view addSubview:c.view];
}
share|improve this answer
he didn't ask how to get the action to the button and then add it to CreateView: CreateView isnt even a view, it is a factory producing views :) – Daij-Djan Jan 30 at 8:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.