jjpp
jjpp

Reputation: 1328

Adding views to the current viewcontroller from another class

I have a UIViewController named MainViewController. I have an another class named ViewMaker. In the ViewMaker class I have a function as follows.

 -(UIView *)GetContentView
{
UIView *return_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];

UIButton *done_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
done_button.frame = CGRectMake(300, 300, 100, 50);
[done_button setTitle:@"Done" forState:UIControlStateNormal];
[return_view addSubview:done_button];
return return_view;
}

In the above function actually I will add more views like textviews, labels etc.

In the MainViewController class I am calling the above method as follows.

 -(void)CreateViews
 {   
 UIView *content_view = [[[ViewMaker alloc] init] GetContentView];
 [self.view addSubview:content_view];
 }

Now I have added a view with done button(and other components like textviews, labels etc) from the MainViewController class. My problem is about adding a target function for the done button. When I click the done button I want to perform some actions(like add some views) in the MainViewController according to the datas in the view that was returned from the ViewMaker class. How can I do this? Thanks in advance.

Upvotes: 1

Views: 419

Answers (3)

ManicMonkOnMac
ManicMonkOnMac

Reputation: 1476

I assume your ViewMaker class is same as PopupContentMaker,

add this to your done button:

[done_button addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];

in PopupContenMaker:

- (void)doSomething{
if ([self.delegate respondsToSelector:@selector(handleDoneButtonTap)]) {
    [self.delegate performSelector:@selector(handleDoneButtonTap) withObject:self];
}

}

declare a delegate property in your PopupContentMaker, and make MainViewController the delegate,

-(void)GetContentView
{   
PopContentMaker *pcm = [[PopupContentMaker alloc] init];

pcm.delegate = self;

[self.view addSubview:[pcm GetContentView]];

}

-(void)handleDoneButtonTap{
//Do action after done button
}

Upvotes: 1

Raphael Oliveira
Raphael Oliveira

Reputation: 7841

You can pass the target and action as parameters to the getContentView method or you can use the delegate pattern.

Upvotes: 0

You can try:

[done_button addTarget: self
      action: @selector(buttonClickedMethod)
forControlEvents: UIControlEventTouchUpInside];

In this way you can call a custom method when button is pressed.

Upvotes: 0

Related Questions