简体   繁体   English

为什么我的Objective-C代码会引发异常?

[英]Why my Objective-C code throws Exception?

I am new to Objective-C and i'm trying to program an iPhone App. 我是Objective-C的新手,我正在尝试编写iPhone App。

This is my code : 这是我的代码:

Head File: 头文件:

#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

and this is the implementation: 这是实现:

#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

When i start the app, it throws an Exception: 当我启动应用程序时,它会引发异常:

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

Can any one help me with my code ? 有人可以帮我做我的代码吗?

And additionally , could you please tell me about those bad written behaviors in my code. 另外,能否请您介绍一下我的代码中的不良书面行为。

Many many thanks ! 非常感谢 !

Connect the root UIView inside your Xib to the File's Owner's view. UIView内的根UIView连接到文件所有者的视图。

在此处输入图片说明

Open up your .xib file, then in Objects click on View, then click on Connections Inspector and from there Control + Click on the circle in Referencing Outlets and drag it to the File's Owner and from the pop-up menu select view .... :) 打开您的.xib文件,然后在``对象''中单击``视图'',然后单击``连接检查器'',然后在其中单击Control +单击``参考插座''中的圆圈并将其拖动到文件的所有者,然后从弹出菜单中选择视图... ::)

连接插座

EDIT:

Your View is the Main UIVIEW, whatever you will put in it or under it in hierarchy will be shown automatically. 您的视图是Main UIVIEW,无论您将其放置在层次结构中还是层次结构下,它都会自动显示。 As you have used UIImageView in it (You can see the hierarchy of views and objects in Objects, column left to xib under the PlaceHolders). 当您在其中使用UIImageView时(您可以在“对象”中的PlaceHolders下xib的左列中查看视图和对象的层次结构)。 When you connect the UIView(parent View) to File's Owner. 当您将UIView(父视图)连接到文件的所有者时。 It shows all other views which are included in the main view. 它显示了主视图中包括的所有其他视图。 So when you connected the main view with File's Owner it resolved your issue. 因此,当您将主视图与文件所有者连接时,它解决了您的问题。

Well, the error message seems pretty clear: 好吧,错误消息似乎很清楚:

loaded the "TutorialViewController" nib but the view outlet was not set.' 加载了“ TutorialViewController”笔尖,但未设置视图出口。

Check the outlets on your TutorialViewController nib. 检查TutorialViewController笔尖上的插座。 Seems like the view outlet is not connected to anything. 好像视图插座没有连接任何东西。 To fix it, connect your top-level-view to the outlet (control drag). 要修复此问题,请将顶级视图连接至插座(控制拖动)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM