我是 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.'
任何人都可以帮我处理我的代码吗?
另外,你能告诉我我的代码中那些糟糕的书面行为吗?
非常感谢 !