按下按钮后 Main.m 崩溃
我刚刚开始使用 X-Code 4.2 构建一个应用程序。我使用单视图应用程序模板创建了该应用程序。
我创建了一个 MainIconViewController,它是一个简单的 VC,其中包含图像、标签和按钮。我将 MainIconViewController 视图添加到我的 mainViewController 视图中,当按下按钮时,Main.m 中出现崩溃,显示:
-[__NSCFType mainButtonPressed:]: unrecognized selector sent to instance 0xb867ba0
Main.m 中发生崩溃的行是:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
现在buttonPressed:完全是空的。
有什么想法如何解决这个问题吗?
我只是尝试制作一个全新的项目来重新测试这一点。我创建了该项目,创建了一个包含单个按钮的 viewController 子类。然后,我在主视图控制器中创建此视图的实例并将其添加到视图中。当我按下按钮时,应用程序像以前一样崩溃。
编辑: 这是所有代码:
//MainIconViewController.h
@protocol MainIconViewControllerDelegate <NSObject>
-(void) openFileNamed:(NSString *)name;
-(void) createNewFile;
@end
#import <UIKit/UIKit.h>
@interface MainIconViewController : UIViewController {
}
@property (assign) id <MainIconViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (void)mainButtonPressed:(id)sender;
@end
//////////////////////////////
//MainIconViewController.m
#import "MainIconViewController.h"
@implementation MainIconViewController
@synthesize titleLabel;
@synthesize imageView;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setTitleLabel:nil];
[self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)mainButtonPressed:(id)sender{
}
@end
I just started building an app using X-Code 4.2. I created the app with the single view application template.
I created a MainIconViewController which is a simple VC which contains an image, label and button. I add the MainIconViewController view to my mainViewController view and when the button is pressed I get a crash in Main.m that says:
-[__NSCFType mainButtonPressed:]: unrecognized selector sent to instance 0xb867ba0
The line in Main.m where the crash occurs is:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
right now buttonPressed: is completely empty.
Any ideas how to fix this?
I just tried making a whole new project to re-test this. I created the project, created a viewController subclass that contained a single button. I then create an instance of this view in the main view controller and add it to the view. When I press the button, the application crashes just like before.
EDIT:
Here's All the code:
//MainIconViewController.h
@protocol MainIconViewControllerDelegate <NSObject>
-(void) openFileNamed:(NSString *)name;
-(void) createNewFile;
@end
#import <UIKit/UIKit.h>
@interface MainIconViewController : UIViewController {
}
@property (assign) id <MainIconViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (void)mainButtonPressed:(id)sender;
@end
//////////////////////////////
//MainIconViewController.m
#import "MainIconViewController.h"
@implementation MainIconViewController
@synthesize titleLabel;
@synthesize imageView;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setTitleLabel:nil];
[self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)mainButtonPressed:(id)sender{
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先你的方法必须像这样声明:
然后你必须像这样调用你的控制器:(
更安全,但是如果你的 xib 文件被命名为像控制器那样就可以了)
当然,不要忘记将你的按钮链接到 IB的行动。
另请检查与
MainIconViewController
对应的 xib 的文件所有者
(在 IB 中)是否设置为 MainIconViewController。请注意,这里有内存泄漏。您可以将控制器保留在您的类中,并在不再需要时将其释放。如果释放得太早,它将崩溃,因为您的视图在需要时内存中没有控制器。如果没有释放(就像这里所做的那样),控制器将继续保留在内存中,并且会出现内存泄漏。
请注意,Apple 不推荐您这样做(一个视图层次结构 = 一个视图控制器)。如果您使用 ARC,这可能是导致崩溃的问题。
First your method have to be declared like this :
Then you must call your controller like this :
(safer, but that's ok if your xib file is named like the controller)
And of course, don't forget to link you button into IB with the action.
Check also that your
File's owner
(into IB) for the xib that corresponds toMainIconViewController
is set to MainIconViewController.Note that you here have a memory leak. You may retain the controller in your class and release it whne not needed anymore. If release too early, it will crash because your view does not have a controller in memory when needed. If not released (like here you do), the controller keeps staying in memory and you have a memory leak.
Note that the way you do is not recommended by Apple (one view hierarchy = one view controller). If you use ARC, that could be the problem of your crash.
正如错误输出中所述,您尚未在头文件中声明方法
mainButtonPressed:
。确保您的签名
在头文件中如下所示:
As said in error output, you haven't declared the method
mainButtonPressed:
in your header file.Make sure your signature looks like:
in your header file.
您如何创建您的视图?您应该通过创建 VC,然后使用 view 属性访问视图,或在视图层次结构上显示视图控制器来完成此操作。
否则,您在界面生成器中建立的连接将不会与此类连接。
How are you creating your view? You should be doing it by creating the VC, then accessing the view with the view property, or displaying the view controller on the view hierarchy.
Otherwise the connections that you make in the interface builder will not connect with this class.
检查此视图控制器的初始化。
Check the initialization of this view controller.