子视图中的IBOutlets为零

时间:2014-04-03 01:57:52

标签: ios objective-c uiviewcontroller xcode5

我有一个UIViewController,它与自定义类MAViewControllerMenu相关联,并在启动画面后立即加载。在UIViewController中,我有一个UIScrollView,它属于另一个类MASlideShowView,其中UIScrollView的IBOutlet已定义并连接到。

enter image description here

UIViewController的类包括以下字段:

@property MASlideShowView* slideShow;

作为保存UIScrollView的类的私有属性。

同样在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

由于

2 个答案:

答案 0 :(得分:2)

在使用Interface Builder时,我建议在initializeImages内调用awakeFromNib

  

将awakeFromNib消息发送到从中加载的每个对象   归档,但只有它可以响应消息,并且只有它毕竟   存档中的对象已加载并初始化。当一个   对象收到一个awakeFromNib消息,它保证拥有所有   它的插座实例变量设置。

更多详情here

其他观察:

至于您的代码,在输入slideShow时,Interface Builder会正确设置viewDidLoad,但您要通过分配_slideShow = [[MASlideShowView alloc] initWithModel]来替换该实例,从而产生完全不同的对象。

此外,您的initWithModel并不像一个正确的init方法,因为它不会调用任何super的{​​{1}}方法。您应该从Apple的代码段开始,将init写在一个空行并按下escape:

enter image description here

enter image description here

同样,答案的第一段应该足以解决您的问题。

答案 1 :(得分:1)

有几种方法可以解决这个问题。

一种方式就像@HoanNguyen提到的那样使用awakeFromNib。我个人不会使用它,但它是一个有效的生命周期事件。

另一种选择是覆盖initWithCoder:这是标准的初始化器故事板使用

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initializeImages];
    }
    return self;
}

然后你可以删除你的initWithModel调用,故事板应该处理所有事情。