ios - subview 中的 IBOutlets 为零

标签 ios objective-c uiviewcontroller xcode5

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

enter image description here

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方法,因为它不调用它的任何 superinit方法。你应该从 Apple 的代码片段开始写 init在一个空行中并按 Escape:

enter image description here

enter image description here

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

关于ios - subview 中的 IBOutlets 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826444/

相关文章:

objective-c - iPad 分屏 View 调用/从另一个 View 加载

iphone - iOS 中的四点渐变

objective-c - NSBlock 对象是如何创建的?

ios - 检测整个屏幕上的触摸

ios - DismissViewControllerAnimated 关闭超过 1 个 Controller

ios - 防止 UICollectionViewCell 的一部分出现 `didSelect…`

ios - iOS 上的 OpenGL ES 2.0 对象拾取(使用颜色编码)

iPhone:使用触摸的应用程序的内存使用量稳步增加

objective-c - 不同情况下带if语句的NSNumber

ios - 如何从 SKScene 返回到 UIViewController (MenuVC)?