我有一个 UIViewController
,与自定义类 MAViewControllerMenu
相关联并在启动画面后立即加载。在那UIViewController
, 我有一个 UIScrollView
,属于另一个类,MASlideShowView
,其中UIScrollView
的IBOutlet被定义并连接到。
UIViewController 的类具有以下字段:
@property MASlideShowView* slideShow;
作为在其中包含 UIScrollView 的类的私有(private)属性。
也在
UIViewController
,- (void)viewDidLoad
{
[super viewDidLoad];
//TODO [_slideShow initializeImages];
_slideShow = [[MASlideShowView alloc] initWithModel];
_slideShow.delegate = imageViewController;
}
- (void)viewDidAppear{
[super viewDidAppear:(YES)];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the content size of the scroll view
//HERE, self.slideShow is allocated, but all the fields it has, including the IBOutlet to the UIScrollView is still nil
CGSize pagesScrollViewSize = self.slideShow.frame.size;
_slideShow.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageViews.count, pagesScrollViewSize.height);
//Delegate
_slideShow.scrollView.delegate = self;
// Load the initial set of pages that are on screen
[_slideShow loadVisiblePages:YES page_index:0 image:_last_image_taken];
}
注意我在上面类的评论中看到的错误
MASlideShowView
文件看起来像:H:
@class MASlideShowView;
@protocol slideShowDelegate <NSObject>
-(void)imageViewSelected:(MASlideShowView*)slideShow image:(UIImage*)image;
@end
@interface MASlideShowView : UIScrollView
@property (nonatomic,weak) id<slideShowDelegate> delegate;//delegate to next controller to notify upon picture centered
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UIButton *rotateImageButton;
@property UIImageView* centered_image_view;
- (IBAction)PageThroughPageControl;
- (IBAction)rotateImageButtonClicked;
- (id)initWithModel;
- (void)pageThroughPageControl;
- (void)addImageToSlideshow:(UIImage*)toAdd;
- (void)loadVisiblePages:(BOOL)use_page_number page_index:(NSInteger)page image:(UIImage*)image;
@end
米:
- (id)initWithModel{
[self initializeImages];
return self;
}
-(void)initializeImages{
// Set up the image you want to scroll & zoom and add it to the scroll view
self.pageViews = [NSMutableArray arrayWithObjects:nil];
NSInteger pageCount = 0;
_imageViewCount = 0;
// Set up the page control
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// Set up the array to hold the views for each page
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
我的问题很简单:
如何制作
UIScrollView
初始化?我知道没有
viewDidAppear
因为它继承自 UIScrollView
.谢谢
最佳答案
当您使用 Interface Builder 时,我建议您调用 initializeImages
里面 awakeFromNib
:
An awakeFromNib message is sent to each object loaded from the archive, but only if it can respond to the message, and only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set.
更多详情 here .
其他观察:
至于你的代码,你有
slideShow
输入 viewDidLoad
时由 Interface Builder 正确设置但是您通过分配 _slideShow = [[MASlideShowView alloc] initWithModel]
来替换该实例,这会导致完全不同的对象。此外,您的
initWithModel
看起来一点也不像正确的 init
方法,因为它不调用它的任何 super
的init
方法。你应该从 Apple 的代码片段开始写 init
在一个空行中并按 Escape:同样,答案的第一段应该足以解决您的问题。
关于ios - subview 中的 IBOutlets 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826444/