Reputation: 26752
So I have a view controller called MainViewController
with a button which when I press this code is called:
NewViewController *newViewController;
newViewController = [[NewViewController alloc] initWithNibName:@"NewView" bundle:nil];
[self.navigationController.view addSubview:newViewController.view];
[newViewController release];
This brings in the new view which works great. However, how can I remove this view from a button within it? In an application I wrote a while ago I simply created a method in MainViewController
called RemoveView
and within the XIB file for NewViewController
I selected FirstResponder
and then RemoveView
for the button. This works but I can not replicate it in my new project and don't really understand how it works anyway!
It's not the remove view code I'm looking for, more the way of getting the method to call from another class.
If anyone could help me that would be great! :)
Thanks
Upvotes: 4
Views: 258
Reputation: 11914
Drawing the line in Interface Builder does the same thing as calling
[theButton addTarget:theController action:@selector(theAction) forControlEvents:UIControlEventTouchUpInside];
theAction needs to be a method that is defined with a type of IBAction
.
For your situation, in your NewViewController.h, declare
- (IBAction)removeView;
Then in NewViewController.m:
- (void)removeView
{
[self.view removeFromSuperview];
}
In your newView.xib file, you should be able to drag a line from the UIButton that you've drawn to your File's Owner, and select the removeView
action.
Upvotes: 2