【问题标题】:loadNibNamed - Could not load NIB in bundle: NSBundleloadNibNamed - 无法在包中加载 NIB:NSBundle
【发布时间】:2013-01-11 00:54:03
【问题描述】:

我正在尝试使用 XIB 文件创建 TableViewCell,但在执行时出现此错误:

2013-01-10 17:54:50.297 MainApp[6778:b603] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在捆绑包中加载 NIB:“NSBundle(已加载)”名称 'DropDownCell''
*
首次抛出时调用堆栈:

这就是我想要做的。
我有一个项目,第一个菜单。在这个项目/工作区中,我有我的第一个菜单的类。在这个项目中,我有另一个工作区,其中包含我的第二个菜单的类。我的意思是,这个工作区是为我的第一个菜单中选择的选项的 SubViewController 和类。在这个工作区中,我正在尝试使用来自苹果的演示创建一个 DropDownMenu,但我的应用程序崩溃了。此演示使用 XIB 文件在表格中创建单元格。
这是 DropDownCell 类:
DropDownCell.h

#import <UIKit/UIKit.h>

@interface DropDownCell : UITableViewCell{
  IBOutlet UILabel *textLabel;
  IBOutlet UIImageView *arrow_up;
  IBOutlet UIImageView *arrow_down;

  BOOL isOpen;
}

-(void)setOpen;
-(void)setClosed;

@property (nonatomic)BOOL isOpen;
@property (nonatomic,retain) IBOutlet UILabel *textLabel;
@property (nonatomic,retain) IBOutlet UIImageView *arrow_up;
@property (nonatomic,retain) IBOutlet UIImageView *arrow_down;

@end

DropDownCell.m #import "DropDownCell.h"

@implementation DropDownCell

@synthesize textLabel, arrow_up, arrow_down, isOpen;

-(void)setOpen{
   [arrow_down setHidden:YES];
   [arrow_up setHidden:NO];
   [self setIsOpen:YES];
}

-(void)setClosed{
   [arrow_down setHidden:NO];
   [arrow_up setHidden:YES];
   [self setIsOpen:NO];
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if(self){

   }
   return self;
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
   [super setSelected:selected animated:animated];
}

-(void)dealloc{
   [super dealloc];
}

@end

DropDownCell.xib 有一个带有 UILabel 的 UITableViewCell。
我有另一个 UITableViewController,它使用 DropDownCell XIB。方法是这样的:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"MenuCell";
  static NSString *DropDownCellIdentifier = @"DropDownCell";

  if([indexPath row] == 0){
      DropDownCell *cell = (DropDownCell*)[tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier];

      if(cell == nil){
          NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"DropDownCell" owner:self options:nil];

          for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[DropDownCell class]]){
                cell = (DropDownCell*)currentObject;
                break;
            }
          }
      }

      [[cell textLabel] setText:@"Option 1"];

      return cell;

}

但我的应用程序在加载 NIB 文件时崩溃了...可能是什么原因?我使用的是 Xcode 4.3,我没有使用故事板。

【问题讨论】:

    标签: objective-c drop-down-menu uitableview xcode4.3


    【解决方案1】:

    @user1600801,可能有以下原因之一:-

    1) 在该自定义单元格的文件检查器中禁用/取消选中“使用自动布局”。

    2) 没有为该自定义单元设置您的目标。

    要设置它,请选择您的自定义单元格 .Xib 文件,

    选择“文件检查器”,

    在“目标成员资格”下检查您的项目名称是否被选中?如果没有,则检查/启用它。

    3) 检查您的自定义单元类名称和单元标识符。

    【讨论】:

    • 同样在属性检查器中确保原型单元格的标识设置为“DropDownCell”
    【解决方案2】:

    您是否为该 NIB 启用了自动布局,并且正在编译

    【讨论】:

    • 我不明白什么或如何拥有自动布局:S...在我的 XIB 文件中,在 File Inspector -> Target Membership 中,我选择了我的 Target...跨度>
    • 在同一个文件检查器选项卡上,在“界面生成器文档”部分,您有一个小复选框,上面写着“使用自动布局”。如果选中它,您的应用程序将在 6.0 之前的 iOS 版本上崩溃。
    • 我没有那个“复选框”。我只有:文档版本控制:部署->项目 SDK 版本(iOS 4.3),开发->接口生成器 3.0 |本地化锁定:默认->无
    【解决方案3】:

    问题是您甚至没有加载捆绑包。看到这个问题:

    NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle

    您似乎可以通过从项目中删除文件并重新放在那里来解决它。

    【讨论】:

    • 我得到同样的错误......由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSBundle loadNibNamed:owner:topLevelObjects:]......另外,我收到了这个警告。 ..“NSBundle 可能无法响应 -loadNibNamed:owner:topLevelObject”...
    • 我稍后对其进行了编辑,在答案的第二部分中您会看到警告的原因。
    • 我尝试使用“initWithNibName”,但也崩溃了,我也收到了警告,因为“UITableViewCell”没有实现“initWithNibName”
    • 我所说的是使用 UIViewController。在视图控制器中,您还加载了 UITableViewCell。
    • 所以,我需要创建另一个 ViewController,比如 DropDownViewController。有了这个 DropDownViewController,有一个带有视图的 XIB 文件和我的 UITableViewCell?并在我的表中加载我的 DropDownViewController,而不是加载 DropDownCell?
    【解决方案4】:

    尝试删除 nib 文件的引用并清理项目。 然后将 nib 文件添加回项目并构建。 这解决了我的项目中发生的相同异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2021-06-15
      • 2018-06-14
      相关资源
      最近更新 更多