【发布时间】: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