gpt4 book ai didi

ios - 将 viewController View 添加到 UIScrollView

转载 作者:行者123 更新时间:2023-11-28 23:06:21 27 4
gpt4 key购买 nike

尽管有很多类似的帖子,但我仍然无法正常工作。我有两个 UIViewControllers,它们是我在 IB 中设计的,带有 UILabels 和 imageviews 等。我想将它们添加到 ScrollView 中,以便可以在它们之间进行分页。

我定义了将每个 scrollViewcontroller 与其在 IB 中的元素连接起来的导出,但是, Controller 本身,我是在 viewDidLoad 的主视图 Controller 中以编程方式创建的。创建它们之后,我从主视图 Controller 分配一些值,而不是将其作为 subview 添加到 scrollViewController。但是没有任何显示。从我所做的调试来看,当我在分配值后设置断点时,我可以看到元素为零。它们应该从 xib 文件中加载,但它似乎没有被读取。即使作为测试,我也尝试初始化其中一个元素(例如 UILabel)并将其设置为该 subview Controller ,但它不会显示任何内容。这里有一些代码可以更好地解释。

在主视图 Controller 中

    detailControllerA *tempA = [[detailControllerA alloc] initWithNibName:@"detailOverviewA" bundle:nil];
self.overviewA = tempA;
[tempA release];
self.overviewA.someLabel.text = @"Some Text";
detailScrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight);
detailScrollView.pagingEnabled = YES;

[detailScrollView addSubview:self.overviewA.view];

在 detailControllerA 实现中,我在 loadView 中设置了框架:

-(void)loadView {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
self.view = view;
[view release];

}

我还在 detailControllerA.h 中使用 IBOutlets 定义了标签等,并实现了 xib 文件中的元素。

知道为什么 xib 在以编程方式创建时无法正确加载吗?

最佳答案

这样试试:

       NSUInteger scrollContentCount = 0;

for (NSUInteger arrayIndex = 0;
arrayIndex < [contents count];
arrayIndex++) {


// set scorllview properties
CGRect frame;
frame.origin.x = self.mScrollView.frame.size.width * arrayIndex;
frame.origin.y = 0;
frame.size = self.mScrollView.frame.size;

myScrollView.autoresizingMask = YES;

// alloc - init PODetailsView Controller
myController = [[MyController alloc] initWithNibName:@"MyController" bundle:[NSBundle mainBundle]];

[myController.view setFrame:frame];

// add view in scroll view
[self.myScrollView addSubview:myController.view];

scrollContentCount = scrollContentCount + 1;
}

// set scroll content size
self.myScrollView.contentSize =
CGSizeMake(self.myScrollView.frame.size.width * scrollContentCount,
self.myScrollView.frame.size.height);
}

不要释放for循环中的content controller对象。

根据您的要求设置您的scrollView contentSize

希望对您有所帮助。

关于ios - 将 viewController View 添加到 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9246465/

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