【问题标题】:Why my Objective-C code throws Exception?为什么我的 Objective-C 代码抛出异常?
【发布时间】:2012-06-06 06:23:44
【问题描述】:

我是 Objective-C 的新手,我正在尝试编写一个 iPhone 应用程序。

这是我的代码:

头文件:

#import <UIKit/UIKit.h>

@interface TutorialViewController : UIViewController{
    UILabel *labelText;
    UIImageView *imageView;
}
@property(nonatomic,retain) IBOutlet UILabel *labelText;
@property(nonatomic,retain) IBOutlet UIImageView *imageView;

-(IBAction) click:(id) sender;

@end

这是实现:

#import "TutorialViewController.h"

@implementation TutorialViewController
@synthesize labelText;
@synthesize imageView;

-(void) click:(id)sender {
    imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
    NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
    NSString *newText = [[NSString alloc]initWithFormat:@"%@",titleOfButton];
    // Change Image When Clicking Color Button
    if([titleOfButton isEqualToString:@"Blue"]){
        NSLog(@"Blue");
        UIImage *image = [UIImage imageNamed:@"1.png"];
        imageView.image = image;
        [image release];
    }else{
        UIImage *image = [UIImage imageNamed:@"2.png"];
        imageView.image = image;
        [image release];
        NSLog(@"Else");
    }
    labelText.text = newText;
    [newText release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
    [super viewDidUnload];
    self.labelText = nil;
}
-(void) dealloc{
    [labelText release];
    [imageView release];
    [super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

当我启动应用程序时,它会抛出异常:

'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "TutorialViewController" nib but the view outlet was not set.'

谁能帮我写代码?

另外,你能告诉我我的代码中那些糟糕的书面行为吗?

非常感谢!

【问题讨论】:

  • 从你分配内存到 TutorialViewController 和加载的地方,你也可以添加那个代码吗?
  • 与您的问题无关,但您有内存管理问题。你正在释放你没有分配、复制或保留的东西。

标签: iphone objective-c ios xcode ios4


【解决方案1】:

将 Xib 中的根 UIView 连接到文件所有者的视图。

【讨论】:

  • 谢谢 Jacky Boy,它确实有效。但在此之前我已经将 UIImageView 连接到 FileOwner 但它不起作用。他们之间有什么关系?
  • 您将UIImageView 连接到文件的所有者,但连接到哪里?到您创建的 IBOutlet?到根视图?
  • @property(nonatomic,retain) IBOutlet UIImageView *imageView;
  • 所以你现在说那行不通。告诉我,imageView 是 nil 吗?
  • 抱歉给您带来了困惑。起初我将 UIImageView 连接到 FileOwner,它不起作用,现在我将 UIView 连接到 FileOwner 并且它可以工作。我不知道为什么。
【解决方案2】:

打开您的 .xib 文件,然后在 Objects 中单击 View,然后单击 Connections Inspector,然后从那里 Control + 单击 Reference Outlets 中的圆圈并将其拖到 File's Owner 并从弹出菜单中选择视图.... :)

EDIT:

您的视图是主 UIVIEW,无论您在其中放置还是在层次结构中放置的任何内容都将自动显示。正如您在其中使用了 UIImageView 一样(您可以在 Objects 中看到视图和对象的层次结构,在 PlaceHolders 下的左列到 xib)。当您将 UIView(父视图)连接到文件的所有者时。它显示了包含在主视图中的所有其他视图。因此,当您将主视图与 File's Owner 连接时,它解决了您的问题。

【讨论】:

  • 好吧,我将 imageView(查看我的代码)连接到 FileOwner...而不是根视图。
  • 您需要将主视图连接到文件的所有者
【解决方案3】:

嗯,错误信息似乎很清楚:

加载了“TutorialViewController”笔尖,但视图出口没有 设置。'

检查 TutorialViewController 笔尖上的插座。似乎视图插座没有连接到任何东西。要修复它,请将您的顶级视图连接到插座(控制拖动)。

【讨论】:

    猜你喜欢
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多