I am basically trying to add two view controllers to one controller.
I created a view based application called “MultipleViews”. After that i add two controller classes “RedView.h” and “BlueView.h” with their own xibs. I am able to add the views of both the controllers to “MutipleViewsViewController” by the method [self.view addSubview:red.view]. Both the views are displayed properly. The problem is when I add a button to the red and blue controllers. Whenever I click the button it says unrecognized selector sent to instance even though I linked the buttons with their functions properly. Am i missing something here?
here is the code:
MultipleViewsViewController.h
#import <UIKit/UIKit.h>
@interface MutipleViewsViewController : UIViewController {
}
@end
MutipleViewsViewController.m
–
(void)viewDidLoad {
[super viewDidLoad];
RedView *red = [[RedView alloc]init];
red.view.frame = CGRectMake(0, 0, 320, 240);
[self.view addSubview:red.view];
BlueView *blue = [[BlueView alloc]init];
blue.view.frame = CGRectMake(0, 240, 320, 240);
[self.view addSubview:blue.view];
}
RedView.h
#import <UIKit/UIKit.h>
@interface RedView : UIViewController {
}
-(IBAction)buttonPressed;
@end
BlueView.h
#import <UIKit/UIKit.h>
@interface BlueView : UIViewController {
}
-(IBAction)buttonPressed;
@end
The buttons are linked to the buttonPressed method through IB.
The message i get when i click the button in the red view is:
MutipleViews[1865:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RedView buttonPressed]: unrecognized selector sent to instance 0x4e12500'
Sorry for not being clear earlier.
The IBActions typically take an input parameter of type id. So your
buttonPressedaction should look likeWhen this action is actually called, a reference to the control which calls it (in this case the button) is passed.
When calling it programatically, you can send the controller’s object (
self) to it.