gpt4 book ai didi

ios - 添加 UIViewController 作为 UIScrollView 的 subview

转载 作者:行者123 更新时间:2023-12-01 16:50:40 25 4
gpt4 key购买 nike

我正在构建一个内部滚动的应用程序。简而言之,这是我的配置:

1)class_1:使用 Collection View 列出的内部包含一组元素(类似于相机行)的 View

2) class_2:由 class_1 调用的模态视图,其中显示了选择的每个奇异元素(来自 class_1)。在同一个类中,我还实现了所有元素的水平滚动

3)class_3:一个简单的viewcontroller,里面有一个imageview,里面将包含我将在class_2中展示的元素

运行我的应用程序看起来 class_3 没有添加为 class_2 的 subview ,并且我尝试加载的所有单一内容都不可见。

下面是我的 class_2 代码

界面:

#import <UIKit/UIKit.h> 
#import "class_3.h"

@interface class_2 : UIViewController

@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) NSArray *imageList;

@end

执行:
@interface class_2 () {
NSMutableArray *_viewControllers;
}

- (void)loadScrollViewWithImage:(NSUInteger)indexImage;

@end

@implementation class_2

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

// _imageList is passed from class_1 when I call class_2. Contains an array of images
self.pageControl.numberOfPages = [_imageList count];
self.pageControl.currentPage = 0;

// I store inside an array N objects as the number f images I have to show. At the beginning the array has only null objects, then it will contains the views based on the images I have scrolled.
//I am storying my images inside a view because maybe I will show more controls and not just the image. It will be much easier manage them.
_viewControllers = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < [_imageList count]; i++) {
[_viewControllers addObject:[NSNull null]];
}

// I set some properties of my scrollview defined as outlet
self.scrollView.frame = CGRectMake(0, 0, 320, 345);
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.scrollView.frame) * [_imageList count], CGRectGetHeight(self.scrollView.frame));
self.scrollView.pagingEnabled = YES;
// this is set to NO, I switched to YES just now for debug (at least I can see how big the area is since there are no images visible)
self.scrollView.showsHorizontalScrollIndicator = YES;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;

// for testing now I always take the first and second elements of my array. I load both so when I move from the first image to the second is already there and there is no gap/flashing effect
[self loadScrollViewWithImage:0];
[self loadScrollViewWithImage:1];
}

#pragma mark - Private methods

- (void)loadScrollViewWithImage:(NSUInteger)indexImage {
// I scrolled until the last image, from here I can only go back
if (indexImage >= [_imageList count])
return;

// I replace the placeholder (null) object
class_3 *controller = [_viewControllers objectAtIndex:indexImage];
if ((NSNull *)controller == [NSNull null]) {
controller = [[class_3 alloc] init];
[_viewControllers replaceObjectAtIndex:indexImage withObject:controller];
}

// I add the controller's view to the scroll view - in case this is the first time I'm scrolling this image
if (controller.view.superview == nil) {
// I set my controller frame as the scrollview works as container
CGRect frame = self.scrollView.frame;
frame.origin.x = CGRectGetWidth(frame) * indexImage;
frame.origin.y = 0;
controller.view.frame = frame;

// I finally add my viewcontroller as child of my superview and subview of the scrollview
[self addChildViewController:controller];
[self.scrollView addSubview:controller.view];
[controller didMoveToParentViewController:self];

// I set an image corrisponding with the element selected from class_1
controller.imageView.image = [_imageList objectAtIndex:indexImage]

// I also tried with a fake image...just in case. No one of them is visible
//controller.imageView.image = [UIImage imageNamed:@"tempPhoto"];
}
}

我的 class_3 是一个 View Controller ,它只承载一个 ImageView ,应该填充之前选择的相应图像。
pagecontroller 用于显示我滚动的位置(letf/right)以及有多少元素。滚动是通过 ScrollView 进行物理管理的。
这是我的 class_3 定义

界面:
#import <UIKit/UIKit.h>
#import "class_3.h"

@interface class_3 : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

实现(没有什么真正令人印象深刻的)
#import "class_3.h"

@interface class_3 ()

@end

@implementation class_3

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.imageView.image = [UIImage imageNamed:@"tempPhoto"];
}

@end

我错过了什么?

肿瘤坏死因子

最佳答案

您的 class_3应该是 UIView , 而不是 UIViewController , 子类。见 here

关于ios - 添加 UIViewController 作为 UIScrollView 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16257264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com