下面是这样的情况:我有一个带有XIB文件的UIViewController。它有一个连接到view属性的UIView,一个连接到名为imageView的属性的UIImageView,以及一个连接到segmentControl的UISegmentedControl。
但在我的代码中,我无法将图像分配给图像视图。当我NSLog对象时,我得到的地址是0x0,所以它根本没有关联。我在Interface builder中设置了绑定,重新启动XCode并清理和构建项目。什么都不管用,我浪费了大量的时间试图解决这个问题。我还对segmentControl对象进行了nslogg,得到了0x0。因此,XIB中没有任何东西与代码连接,但是XIB本身正在加载,因为对象显示在屏幕上。
有人知道我会错过什么吗?这是一件很简单的事情,它突然拒绝工作让人难以置信的沮丧。
下面是代码,在本例中,我正在为projectId访问setter中的对象,但我也尝试从另一个类访问imageView,但它也不起作用(是的,我尝试时有一个@property声明并为imageView进行合成)。
图表视图控制器.h:
#import <UIKit/UIKit.h>
@interface DiagramViewController : UIViewController
{
NSUInteger projectId;
IBOutlet UIImageView *imageView;
IBOutlet UISegmentedControl *segmentControl;
}
@property NSUInteger projectId;
@end
图表视图控制器.m:
#import "DiagramViewController.h"
@implementation DiagramViewController
@synthesize projectId;
-(void)setProjectId:(NSUInteger)projId
{
NSLog(@"imageView: %@", imageView);
NSLog(@"segmentControl: %@", segmentControl);
projectId = projId;
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%i_schem", projectId] ofType:@"png" inDirectory:@"project-details/images/diagrams"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
imageView.image = [UIImage imageWithData:imageData];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
以下是界面生成器连接的图像: