【问题标题】:Crash in Main.m after pressing button按下按钮后在 Main.m 中崩溃
【发布时间】:2011-10-29 17:36:49
【问题描述】:

我刚开始使用 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

【问题讨论】:

  • 其实崩溃不在main()中,贴一下按钮动作代码。
  • 按钮动作中什么都没有

标签: iphone ios xcode ipad crash


【解决方案1】:

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

- (IBAction)mainButtonPressed:(id)sender;

那么你必须像这样调用你的控制器:

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

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

当然,不要忘记将您的按钮链接到带有操作的 IB。

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

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

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

【讨论】:

  • 感谢您将我引向错误 - 出于某种原因,ARC 在语言上关闭并在我的目标中打开警告。我都打开了,它工作正常。
【解决方案2】:

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

确保您的签名看起来像:

- (void)mainButtonPressed:(id)sender;

在你的头文件中。

【讨论】:

  • ...或者该方法可能不存在。严格来说,您不需要在头文件中声明该方法,如果您错过了它并且该方法仍在您的实现中,您将不会收到无法识别的选择器错误。更有可能该方法根本不存在,或者存在拼写错误。
  • 该方法存在并且声明与您输入的完全相同
  • 在某些情况下,编译器会发出警告,告诉选择器可能不存在(例如 SomeClassName 可能不会响应选择器 SelectorName )。为了避免警告,我更喜欢在 header 中编写所有选择器。
【解决方案3】:

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

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

否则您在界面构建器中建立的连接将无法与此类连接。

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 我需要检查什么?这里是: MainIconViewController *icon = [[MainIconViewController alloc]initWithNibName:nil bundle:nil]; [viewForIcons addSubview:icon.view]; icon.view.frame = CGRectMake(40, 20, icon.view.frame.size.width, icon.view.frame.size.height);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多