我基本上是在尝试将两个视图控制器添加到一个控制器。
我创建了一个名为“MultipleViews”的基于视图的应用程序。之后,我添加了两个控制器类“RedView.h”和“BlueView.h”以及它们自己的 xib。我可以通过方法将两个控制器的视图添加到“MutipleViewsViewController” [self.view addSubview:red.view]
。两个视图都正确显示。问题是当我向红色和蓝色控制器添加一个按钮时。每当我单击按钮时,unrecognized selector sent to instance
即使我将按钮与其功能正确链接,它也会显示。我在这里错过了什么吗?
这是代码:
MultipleViewsViewController.h
#import <UIKit/UIKit.h>
@interface MutipleViewsViewController : UIViewController {
}
@end
多视图视图控制器.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];
}
红视图.h
#import <UIKit/UIKit.h>
@interface RedView : UIViewController {
}
-(IBAction)buttonPressed;
@end
蓝视图.h
#import <UIKit/UIKit.h>
@interface BlueView : UIViewController {
}
-(IBAction)buttonPressed;
@end
按钮通过 IB 链接到 buttonPressed 方法。单击红色视图中的按钮时收到的消息是:
MutipleViews[1865:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RedView buttonPressed]: unrecognized selector sent to instance 0x4e12500'
很抱歉之前没有说清楚。