【问题标题】:Exception : Could not load NIB in bundle异常:无法在捆绑包中加载 NIB
【发布时间】:2014-08-24 04:32:41
【问题描述】:

我是 iOS 及其开发的新手。我正在开发 iPhone 应用程序。我有 UITableView 和 UIWebView。当我按下 UITableview 的单元格时,点击那里我想在我的 UIWebView 中加载各种网页。请在下面找到我遇到的错误。

    2014-07-05 08:25:59.319 WADTourisum[432:60b] myUrl-->2 
2014-07-05 08:26:02.753 WADTourisum[432:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main.storyboard' in bundle NSBundle </Users/venushka/Library/Application Support/iPhone Simulator/7.1/Applications/3325DB09-FC4D-4B28-8DAF-5F9ABCAB8464/WADTourisum.app> (loaded)'
*** First throw call stack:
(
    0   CoreFoundation                      0x01a421e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0158f8e5 objc_exception_throw + 44
    2   UIKit                               0x007b3400 -[UIStoryboard name] + 0
    3   WADTourisum                         0x0001411e -[Essentialinfocontroller tableView:didSelectRowAtIndexPath:] + 174
    4   UIKit                               0x003399a1 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1513
    5   UIKit                               0x00339b14 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 279
    6   UIKit                               0x0033e10e __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
    7   UIKit                               0x0026d0aa ___afterCACommitHandler_block_invoke + 15
    8   UIKit                               0x0026d055 _applyBlockToCFArrayCopiedToStack + 403
    9   UIKit                               0x0026ce76 _afterCACommitHandler + 532
    10  CoreFoundation                      0x01a0a36e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    11  CoreFoundation                      0x01a0a2bf __CFRunLoopDoObservers + 399
    12  CoreFoundation                      0x019e8254 __CFRunLoopRun + 1076
    13  CoreFoundation                      0x019e79d3 CFRunLoopRunSpecific + 467
    14  CoreFoundation                      0x019e77eb CFRunLoopRunInMode + 123
    15  GraphicsServices                    0x03a365ee GSEventRunModal + 192
    16  GraphicsServices                    0x03a3642b GSEventRun + 104
    17  UIKit                               0x0024ff9b UIApplicationMain + 1225
    18  WADTourisum                         0x00004d1d main + 141
    19  libdyld.dylib                       0x02089701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Essentialinfocontroller.m

    #import "Essentialinfocontroller.h"

        @interface Essentialinfocontroller ()
        @end

        @implementation Essentialinfocontroller

        @synthesize myWebview;

        @synthesize courses;//dictionary type object
        @synthesize coursekeys; //array type object



        - (void)viewDidLoad
        {
            [super viewDidLoad];
            NSString *myfile= [[NSBundle mainBundle]pathForResource:@"essentialinfo" ofType:@"plist"];

           // NSLog(@"testing path %@",myfile);

            courses=[[NSDictionary alloc]initWithContentsOfFile:myfile];

            coursekeys =[courses allKeys];

        }

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            return [courses count];
        }


        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            UITableViewCell * cell=[[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

            NSString * currentCourseName =[coursekeys objectAtIndex:[indexPath row]];

            [[cell textLabel]setText:currentCourseName];

            cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

            return  cell;
        }

        //implement stack method

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {



            if(indexPath.row==0){


        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];

        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"story"];

        [self.navigationController pushViewController:viewController animated:YES];


    }
    if (indexPath.row==1) {
        NSLog(@"myUrl-->2 ");

    }


        }

    //end




    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];

    }

@end

DetailViewController.m

@implementation DetailViewController

@synthesize info;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url= [NSURL URLWithString:@"http://www.google.lk"];
    NSURLRequest * requestURL= [NSURLRequest requestWithURL:url];
    [info loadRequest:requestURL];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

@end

【问题讨论】:

  • 你在使用 Storyboard 吗?你有一个名为 DeatailViewController 的 xib 文件吗?
  • @ali59a 我正在使用storyboard。我没有这样的文件
  • 你的故事板中是否有一个带有 DeatailViewController 类的视图控制器?
  • 太棒了。如果您使用情节提要,则不应使用 initWithNib。您可以直接在情节提要中使用 segue 将表格视图的单元格关联到详细视图控制器,或者您可以在情节提要中为您的详细视图控制器提供标识符并进行以下操作: DetailViewController *temp = [[UIStoryboard storyboardWithName:@"Name你的故事板“捆绑:nil] instantiateViewControllerWithIdentifier:@"Identifier of Detail VC"];
  • 如果你在storyboard中使用,你不能使用加载nib文件,它总是显示错误,你需要用标识符名称或特定VC名称的故事板名称来识别,使用这个链接@ 987654321@

标签: ios iphone objective-c uitableview uiwebview


【解决方案1】:

这一行引发了异常:

DetailViewController *temp =[[DetailViewController alloc]initWithNibName:@"DeatailViewController" bundle:[NSBundle mainBundle]];

上面的行用一个 nib 文件(.xib)初始化一个视图控制器

正如您在评论中所说,您正在使用情节提要,因此您应该使用UIStoryboard Class

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];

UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController identifier"];

现在,获取视图控制器的标识符:

  1. 打开故事板
  2. 单击您的视图控制器(此处为详细视图控制器)
  3. 在右侧:实用程序,单击显示身份检查器(第三个按钮)
  4. 在情节提要 ID 字段中,输入标识符(例如 DetailViewController 标识符)

编辑:

我认为您的故事板的名称是错误的。在您的项目中,您有一个名为 Main.storyboard 或 somethingElse.storyboard 的文件,因此您应该在这一行中替换 storyboard :

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"storyboard" bundle:nil]; by Main 或 somethingElse 不带 .storyboard 扩展名

然后您将这一行 UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController identifier"]; 中的 DetailViewController identifier 替换为您的视图控制器的标识符(屏幕截图中的 story)

【讨论】:

  • 感谢您的提前。现在我有我的错误,但问题是当来到 if(indexPath.row==0){ } // 代码部分所需的 DetailView 控制器没有按原样导航。我该如何修复它。我想我'我在获取 id 和故事板时遇到问题 -> ViewController 的 UIWebview
  • 你能用新代码的版本更新你的帖子吗?
  • 你能找到我编辑过的吗。我仍然无法解决故事板问题。谢谢你到目前为止的进步。
  • 你应该把 Main 而不是 Main.storyboard 放在这一行: UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
  • AB 谢谢你的进步,它对我有用。我之前的投票已经接受了你的回答。非常感谢(Y)
【解决方案2】:
DetailViewController

@property(nonatomic,retain)NSString *myurl;



- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url= [NSURL URLWithString:myurl];
    NSURLRequest * requestURL= [NSURLRequest requestWithURL:url];
    [info loadRequest:requestURL];

}


Essentialinfocontroller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if(indexPath.row==0){ //what ever you want row 


     DetailViewController *temp =[[DetailViewController alloc]initWithNibName:@"DeatailViewController" bundle:nil];

     //or [[DetailViewController alloc]init];


     temp.myurl = @"http://www.google.lk";

      [self.navigationController pushViewController:temp  animated:YES];

}


}

【讨论】:

    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 2017-11-01
    • 2016-11-21
    • 1970-01-01
    • 2013-07-26
    相关资源
    最近更新 更多