iPad / iPhone多方位最佳实践? [英] iPad/iPhone multiple orientations best practice?

查看:78
本文介绍了iPad / iPhone多方位最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的观点转换为适合任何方向的工作(特别是因为它推荐用于iPad)。我一直在用IB来解决问题,但我不确定如何最好地进行。

I'd like to convert my views to work for any orientation (especially since it recommended for iPad). I've been using IB to lay things out, and am not sure how to best proceed.

我理想的是在IB中旋转视图,重做布局,并将两个方向保存到同一个XIB,以便视图自动知道当做什么时方向改变。这似乎不太可能。

What I would ideally like is to rotate the view in IB, redo the layout, and save both orientations to the same XIB so that the view automatically knows what to do when the orientation changes. This doesn't seem possible.

我知道当方向改变时我可以在代码中重新排列视图,但是使用IB没有多大意义,因为其中一个我的主要优点是将所有丑陋的布局代码从我的逻辑中分离出来。

I know I can rearrange the views in code when the orientation changes, but then there's not much point in using IB, since one of its main advantages for me has been to separate out all that ugly layout code from my logic.

其他人为此做了什么?他们只是设计他们的视图,以便UIViewAutoResizing标志可以适当地处理旋转吗?他们是否为每个方向都有多个视图,并以某种方式平滑地切换它们?

What do others do for this? Do they just design their views so that the UIViewAutoResizing flags can handle rotations appropriately? Do they have multiple views for each orientation and somehow switch these out smoothly?

推荐答案

我在这里(以及StackOverFlow上的其他地方)找到了这些信息关于这个主题),它让我思考我以前的答案。如果你有很多顶级对象,这就变得乏味。

I found the information here (and elsewhere on StackOverFlow on this topic), and it got me to thinking about my previous answer. This just gets tedious if you have lots of top level objects.

然后,我发现你可以创建一个帮助对象,所以我创建了一个项目演示技术。基本上,帮助者在视图中具有针对每个感兴趣对象的出口。 ViewController本身有两个Helper对象(一个用于p,一个用于l)并且您在NIB中实例化它们。在方向更改时,将视图控制器的帮助程序指针切换到相应的帮助程序,然后更新self.view。它非常简单(这个文字更难)。

Then, it occurred to me that you could create a "helper" object, so I created a project to demonstrate the technique. Basically, the helper has an outlet for each object of interest in the view. The ViewController itself has two Helper objects (one for p, one for l) and you instantiate those in the NIB. On orientation changes, you switch the view controller's helper pointer to the appropriate helper, then update self.view. Its really simple (this text is harder).

那么,你会如何开始?如果您事先知道需要两个单独的视图,那么它会有所帮助。知道了这一点后,在下面引用的项目中使用Helper模板,对其进行调整,将其添加到NIB中,然后将帮助连接到纵向视图中的相应对象。 View Controller本身只是对两个助手的引用,以及一个curHelper指针。 Helper ivars是公共的,因此View Controller可以引用诸如curHelper-> label1.text = ...之类的项目 - 成本是单指针解除引用(也可以只使用属性 - 你的调用)。

So, how would you start? Well it helps if you know a priori that you will need two separate view. Once you know that, take the Helper template in the referenced project below, adapt it, add it to your NIB, and then connect the help to the appropriate objects in the portrait view. The View Controller itself just has a reference to the two helpers, and a "curHelper" pointer. The Helper ivars are public, so the View Controller can reference items like "curHelper->label1.text = ... - the cost is a single pointer dereference (could just use properties too - your call).

此外,你可以在任一视图中直接连接对象的IBActions。所以,让我们来看看:

Also, you can directly connect IBActions from the objects in either view. So, lets walk through this:


  • 创建NIB

  • 创建一个Helper对象,并在nib中实例化一个名为Portrait

  • 将所有IBOutlets添加到Helper对象,句柄然后在dealloc中,viewWillUnload

  • 在视图控制器中正常放置IBActions

  • 连接NIB - Helper-> Portrait视图插座,操作到查看控制器

  • 让它全部在Portrait中工作

  • 添加一个新的Helper对象,称之为Landscape

  • 复制当前纵向视图,旋转它并保存它

  • 将景观助手的出口连接到新的横向视图(已为您设置的操作!)

  • create the NIB
  • create a Helper object, and instantiate one in the nib called Portrait
  • add all the IBOutlets to the Helper object, handle then in dealloc, viewWillUnload
  • put IBActions as normal in the View Controller
  • wire up the NIB - Helper->Portrait view outlets, actions to the View Controller
  • get it all working in Portrait
  • add a new Helper object, call it Landscape
  • dup the current portrait view, rotate it, and save it
  • wire up the landscape Helper's outlets to the new landscape view (actions already set for you!)

显然你需要从现在起进行重复更改,但在任何双笔尖方案中你都必须这样做。在上面的技术中,所有插座名称都保持不变。

Obviously you need to from now on make duplicate changes, but in any dual nib scheme you would have to do that. In the above technique, all outlet names stay the same.

View Controller需要在接收到它时向两个助手发送viewDidUnload,并释放助手(然后解除他们的出口。)

The View Controller needs to send "viewDidUnload" to both helpers when it receives it, and to dealloc the Helpers (which then dealloc their outlets).

注意:我只是将它们放在一起看看它看起来如何。如果你想看到代码和一个非常小的演示,你可以从名为Helpers.zip的iPhone文件夹中的public dot me dot com slash dhoerl下载它。如果需要,我会更新此文本和此文本。

NOTE: I just put this together to see how it looked. If you want to see the code and a really small demo, you can download it from public dot me dot com slash dhoerl, in the iPhone folder with name Helpers.zip. I'll update this and this text if needed.

这篇关于iPad / iPhone多方位最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆