我是新來的Objective-C,我試圖編寫一個iPhone應用程序。爲什麼我的Objective-C代碼拋出異常?
這是我的代碼:
頭文件:
#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.'
Can any on e幫我用我的代碼?
此外,您能否告訴我關於我的代碼中的不良書面行爲。
很多很多謝謝!
從你分配內存到TutorialViewController和加載的地方,你可以添加代碼嗎? – rishi
與您的問題無關,但您的內存管理問題存在。你釋放你沒有分配,複製或保留的東西。 – Chuck