UIScrollView 均由 UIViewController 的“视图”引用;财产和出口
我即将向我的 iPhone 项目添加一个 UIScrollView
,在实现此功能之前,我想检查一下我的方法是否正确,或者我是否可能违反了一些我不知道的最佳实践的。
我见过的教程通常涉及将 UIScrollView
添加到现有的 UIView
中,然后它们就从那里开始工作。但是,我想知道是否可以完全保留 UIView
并将 UIScrollView
添加为我的 nib 文件中唯一的顶级对象。
我的示例项目使用 Xcode 的基于视图的应用程序模板:
项目导航器 http://img542.imageshack.us/img542/5364/projectnavigator.png< /a>
我从原始中删除了 UIView
顶级对象MySampleViewController.xib
文件并通过添加 UIScrollView
对象替换它:
笔尖占位符 http://img526.imageshack.us/img526/7709/placeholderobjects.png< /a>
现在我的 nib 文件仅在画布中显示该对象:
画布 http://img233.imageshack.us/img233/4063/scrollview.png
然后我创建了从 UIViewController
的 view
出口到UIScrollView
。
现在,如果我想以编程方式操作 UIScrollView 的内容,我可以使用以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
// Solution B: With the following line we avoid creating an extra outlet linking to the UIScrollView top-level object in the nib file
UIScrollView *scrollView = (UIScrollView *)self.view;
for (int i = 0; i < colors.count; i++) {
CGRect frame;
//frame.origin.x = self.scroller.frame.size.width * i; // Solution A: scroller is an IBOutlet UIScrollView *scroller;
frame.origin.x = scrollView.frame.size.width * i; // Solution B
frame.origin.y = 0;
//frame.size = self.scroller.frame.size; // Solution A
frame.size = scrollView.frame.size; // Solution B
UIView *subView = [[UIView alloc] initWithFrame:frame];
subView.backgroundColor = [colors objectAtIndex:i];
//[self.scroller addSubview:subView]; // Solution A
[self.view addSubview:subView]; // Solution B
[subView release];
}
//self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width * colors.count, self.scroller.frame.size.height); // Solution A
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * colors.count, scrollView.frame.size.height); // Solution B
}
为了实现解决方案 A,滚动器出口也必须链接到笔尖的 UIScrollView,连接检查器如下所示:
连接检查器 http://img228.imageshack.us/img228/8397/connectionsj.png< /a>
解决方案 A 需要一个插座,这意味着有两个到 UIScrollView
的连接: UIViewController
自己的 view
出口和 MySampleViewController
的 scroller
出口。两个媒体指向同一观点是否合法和/或建议?
解决方案 B 仅涉及 UIViewController
的 view
出口链接到视图,并使用此行:
UIScrollView *scrollView = (UIScrollView *)self.view;
我的问题:
- 我是否会发生某种情况使用这两种解决方案之一是否违反了 Apple 的设计准则?
- 我应该在 UIView 解决方案中坚持使用 UIScrollView 吗?
- 还有其他方法可以实现吗?
谢谢!
PS 抱歉语法突出显示,所以无法识别“objective-c”标签的使用
I'm about to add a UIScrollView
to my iPhone project and before I implement this functionality I wanted to check if my approach is the right one or if I could be violating some best practice I'm not aware of.
The tutorials I've seen generally involve adding a UIScrollView
to an existing UIView
and they work from there. However, I was wondering if I could spare the UIView
altogether and just add the UIScrollView
as the only top-level object in my nib file.
My sample project uses Xcode's View-based Application template:
Project navigator http://img542.imageshack.us/img542/5364/projectnavigator.png
I deleted the UIView
top-level object from the original MySampleViewController.xib
file and replaced it by adding a UIScrollView
object:
Nib placeholders http://img526.imageshack.us/img526/7709/placeholderobjects.png
Now my nib file only shows this object in the canvas:
Canvas http://img233.imageshack.us/img233/4063/scrollview.png
Then I created the link from the UIViewController
's view
outlet to the UIScrollView
.
Now, if I wanted to programmatically manipulate the contents of the UIScrollView I can use this code:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
// Solution B: With the following line we avoid creating an extra outlet linking to the UIScrollView top-level object in the nib file
UIScrollView *scrollView = (UIScrollView *)self.view;
for (int i = 0; i < colors.count; i++) {
CGRect frame;
//frame.origin.x = self.scroller.frame.size.width * i; // Solution A: scroller is an IBOutlet UIScrollView *scroller;
frame.origin.x = scrollView.frame.size.width * i; // Solution B
frame.origin.y = 0;
//frame.size = self.scroller.frame.size; // Solution A
frame.size = scrollView.frame.size; // Solution B
UIView *subView = [[UIView alloc] initWithFrame:frame];
subView.backgroundColor = [colors objectAtIndex:i];
//[self.scroller addSubview:subView]; // Solution A
[self.view addSubview:subView]; // Solution B
[subView release];
}
//self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width * colors.count, self.scroller.frame.size.height); // Solution A
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * colors.count, scrollView.frame.size.height); // Solution B
}
In order to implement Solution A the scroller outlet must be linked to the nib's UIScrollView as well, and the Connections Inspector looks like this:
Connections Inspector http://img228.imageshack.us/img228/8397/connectionsj.png
Solution A requires an outlet and this means having two connections to the UIScrollView
: the UIViewController
's own view
outlet and MySampleViewController
's scroller
outlet. Is it legal and/or recommended to have two outlets pointing to the same view?
Solution B only involves UIViewController
's view
outlet linking to the view, and using this line:
UIScrollView *scrollView = (UIScrollView *)self.view;
My questions:
- Do I incur in some sort of violation of Apple's design guidelines by using one of these two solutions?
- Should I stick to the UIScrollView within a UIView solution?
- Is there any other way to implement this?
Thanks!
P.S. Sorry for the syntax highlight, SO didn't recognize the use of the 'objective-c' tag
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,我认为你都很好。
我会,我不认为 UIView 有任何显着的成本,另外如果你想添加页面控件怎么办?而且您不必每次需要时都将控制器的视图转换为 UIScrollView。
看起来你已经掌控了我。
标准做法是让 IBOutlet 指向您想要直接从视图控制器访问的 .nib 中定义的任何视图。
如果您不需要两个出口,您可以给滚动视图一个标签,然后像这样找到它:
然后您只有视图作为出口,但我只会添加额外的出口。只需确保在 viewDidUnload 中将它们设置为 nil 即可。
此外,您不必保留滚动视图(如果您仍在使用保留/释放)。由于滚动视图位于视图控制器的视图内,因此它保留了引用,因此如果您使用 ARC,您可以通过分配或周来拥有滚动视图的属性。
希望有帮助。
No I think you are fine either way.
I would, I don't think a UIView has any significant cost, plus what if you want to add a page control? and you don't have to cast the controller's view to a UIScrollView every time you need it.
Looks like you have it under control to me.
It standard to have IBOutlets to any view defined in your .nib that you want to access directly from your view controller.
If you don't want two outlets you could give the scroll view a tag then find it like so:
Then you only have the view as an outlet, but I would just add the extra outlet. Just make sure you set them to nil in your viewDidUnload.
Also you don't have to retain the scroll view (if you are even still using retain/release). Since the scroll view is inside your view controller's view it keeps a reference so you can have your scrollview's property by assign or week if your using ARC.
Hope that helps.