按下按钮后在Main.m中崩溃

时间:2011-10-29 17:36:50

标签: iphone ios xcode ipad crash

我刚开始使用X-Code 4.2构建应用程序。我使用单一视图应用程序模板创建了应用程序。

我创建了一个MainIconViewController,它是一个包含图像,标签和按钮的简单VC。我将MainIconViewController视图添加到我的mainViewController视图中,当按下按钮时,我在Main.m中遇到崩溃:

- [__ NSCFType mainButtonPressed:]:无法识别的选择器发送到实例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

4 个答案:

答案 0 :(得分:0)

如错误输出中所述,您尚未在头文件中声明方法mainButtonPressed:

确保您的签名如下:

- (void)mainButtonPressed:(id)sender;

在你的头文件中。

答案 1 :(得分:0)

检查此视图控制器的初始化。

答案 2 :(得分:0)

首先,你的方法必须像这样声明:

- (IBAction)mainButtonPressed:(id)sender;

然后你必须像这样召唤你的控制器:

MainIconViewController *icon = [[MainIconViewController alloc]initWithNibName:@"your xib name without extension" bundle:nil];

(更安全,但如果你的xib文件被命名为控制器那就没关系)

当然,不要忘记通过动作将您的按钮链接到IB。

还要检查与File's owner对应的xib的MainIconViewController(进入IB)是否设置为MainIconViewController。

请注意,此处有内存泄漏。您可以将控制器保留在您的班级中,并在不再需要时将其释放。如果过早发布,它将崩溃,因为您的视图在需要时内存中没有控制器。如果没有释放(就像你这样做),控制器会一直停留在内存中并且你有内存泄漏。

请注意,Apple不推荐您这样做(一个视图层次结构=一个视图控制器)。如果您使用ARC,那可能是您崩溃的问题。

答案 3 :(得分:0)

您是如何创建视图的?您应该通过创建VC,然后使用view属性访问视图,或在视图层次结构上显示视图控制器来完成此操作。

MainIconViewController *vc = [[MainIconViewController alloc]initWithNibName:@"MainIconViewController" bundle:nil];
[self.view addSubview:vc.view];
// or....
[self presentModalViewController:vc animated:NO];

否则,您在界面构建器中建立的连接将不会与此类连接。