将 viewController 视图添加到 UIScrollView
尽管有很多类似的帖子,但我仍然无法正确地完成这项工作。 我有两个 UIViewController,我在 IB 中设计了 UILabels 和 imageviews 等。 我想将它们添加到滚动视图中,以便可以在它们之间进行分页。
我定义了将每个滚动视图控制器与其在 IB 中的元素连接起来的出口,但是,控制器本身是我从 viewDidLoad 中的主视图控制器中以编程方式创建的。创建它们后,我从主视图控制器分配一些值,而不是将其作为子视图添加到scrollViewController。然而什么也没有显示。从我所做的调试来看,当我在分配值后设置断点时,我可以看到元素为零。它们应该从 xib 文件加载,但似乎没有被读取。即使作为测试,我也尝试初始化其中一个元素(例如 UILabel)并将其设置为该子视图控制器,但它不会显示任何内容。这是一些代码可以更好地解释。
在主视图控制器中,
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 时无法正确加载,有什么想法吗?
Despite the number of similar posts on this, I still am having trouble making this work correctly.
I have a two UIViewControllers which I have designed in IB with UILabels and imageviews, etc.
I would like to add these to a scroll view so they can be paged between.
I have outlets defined connecting each of the scrollViewcontrollers with their elements in IB, however, the controllers themselves, I am programatically creating from within the master view controller in viewDidLoad. After creating them, I assign some values from the master view controller than add it as a subview to the scrollViewController. However nothing displays. From the debugging i have done, when I set a break point after assigning the values, I can see that the elements are nil. They should be loaded from the xib file, but it does not seem to be read. Even as a test, I tried initializing one of the elements (eg. UILabel) and setting it to that sub view controller, but it nothing would display. Here is some code to explain a bit better.
In the master view 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];
In the detailControllerA implementation I set the frame in loadView:
-(void)loadView {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
self.view = view;
[view release];
}
I also have the labels, etc defined with IBOutlets in detailControllerA.h and implemented to the elements in the xib file.
Any ideas why the xib is not loading correctly when created programatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样:
不要释放 for 循环中的内容控制器对象。
根据您的要求设置您的 scrollView contentSize。
希望这对您有帮助。
Try in this way:
Don't release the content controller object which is in for-loop.
Set your scrollView contentSize according to your requirement.
Hope this would be helpful to you.
有趣的是, -(void)loadView 是问题所在。
由于视图是在界面生成器中生成的(使用框架设置),因此当 loadView 触发时,它会创建一个新视图,从而消除在界面生成器中创建的视图。
Interestingly enough, the -(void)loadView was the problem.
Since the view is being generated in interface builder (with the frame settings), when loadView fires, it was creating a new view, blowing away the one created in interface builder.