4

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

ingh.am
  • 25,981
  • 43
  • 130
  • 177

1 Answers1

2

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.

GendoIkari
  • 11,734
  • 6
  • 62
  • 104
  • How would you setup `theController`? – ingh.am Nov 22 '10 at 21:54
  • theController is your newViewController in that case. It is whatever class contains the method that you wish to call. – GendoIkari Nov 22 '10 at 22:05
  • But the method is in the first view controller, `MainViewController`. So how can I reference it? – ingh.am Nov 22 '10 at 22:21
  • If you are adding the target in code, instead of in IB (using addTarget), then `theController` would be `self`. You'll need to make a property accessor for your button so you can access it from your mainViewController. – GendoIkari Nov 22 '10 at 22:23
  • The button is owned by newViewController, but the method it will call is in mainViewController. I was thinking of sending the id through when I add the view but that didn't work. – ingh.am Nov 22 '10 at 22:25
  • in newViewController, make a property getter that returns your button. Then you can access is from mainViewController. Just make sure your property is an IBOutlet, and it is connected to the Button that you've drawn in IB. – GendoIkari Nov 22 '10 at 22:26
  • Or you can have newViewController have a reference to mainViewController, and add the target itself in viewDidLoad. Either way should work the same. – GendoIkari Nov 22 '10 at 22:27
  • I've just setup the property to the button but still not responding :( I'm now setting the target after declaring newViewController like so: `[newViewController.backButton addTarget:self action:@selector(RemoveView) forEvent:UIControlEventTouchUpInside];`. Its giving me a warning saying that it may not respond to -addTarget :/ – ingh.am Nov 22 '10 at 22:37
  • That should work... backButton returns a UIButton? Also make sure that you call that after you actually add the subview. The button is nil until then. Is RemoveView the name of a method in MainViewController? You normally wouldn't capitalize the first letter of a method name... – GendoIkari Nov 22 '10 at 22:41
  • Yea removeView is a method in MainViewController which I want to call from newViewController. I've done this with firstResponder in the past but it's just not working here and I can't see any differences! – ingh.am Nov 22 '10 at 22:51
  • Got it to work. Change forEvent in your answer to forControlEvents! – ingh.am Nov 23 '10 at 19:35