管理iPad端口中的View Controllers [英] Managing View Controllers in iPad port

查看:187
本文介绍了管理iPad端口中的View Controllers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我为iPhone开发的应用程序,但现在想要移植到iPad。 iPhone应用程序是导航风格,基于由导航控制器管理的离散表视图控制器。 iPad的大屏幕空间意味着我可以同时轻松地将几个桌面视图控制器安装到屏幕上。

I've got an app that I've developed for the iPhone, but now want to port to the iPad. The iPhone app is navigation style and based on discrete table view controllers managed by a nav controller. The larger screen real estate of the iPad means that I can comfortably fit a couple of these table view controllers on to the screen at the same time.

问题是如何?我应该

a)让主视图从不同的NIB加载两个表视图控制器,然后将它们放在屏幕上(我不确定如何设置它们x和y从nibs加载的子视图)。

a) have the main view load two table view controllers from separate NIBs and then position them on screen (I'm not sure how I set they x and y of subviews loaded from nibs).

b)在我的主nib中创建子视图,并用我现有类中的数据填充这些子视图(如果是这样,我如何连接IBOutlets)?

b) create sub-views in my main nib and populate these with data from my existing classes (if so how do I hook up the IBOutlets)?

c)做一些完全不同的事情

c) do something completely different

我应该指出的一件事是我不要我想使用分屏选项。

One thing I should point out is that I don't want to use the split screen option.

推荐答案

提醒!这个QA现在只具有历史价值。

现在用iOS中的容器视图做这种事情是微不足道的,这就是Apple编辑它们的原因:

It is now trivial to do this sort of thing with container views in iOS, which is why Apple edited them:

https://stackoverflow.com/a/25910881/294884

如何在Objective-C中添加一个拥有自己的UIViewController的子视图?

历史回答......

Historic answer...

..我如何设置从笔尖加载的子视图的x和y?

".. how I set they x and y of subviews loaded from nibs?"

我不确定我是否完全理解你的问题Phil,但这里有一个简单明了的方法:

I'm not sure if I fully understand your question Phil, but here's an easy and clear way:

启动界面在新的大型iPad视图中,只需添加新的较小视图(UIViews)......将它们准确地放在您想要的位置和方式中。我们将称之为篮子视图。

Fire up interface builder and in the new larger iPad view, simply add new smaller views (UIViews)... Put them exactly where and how you want them. We are going to call these "basket" views.

让我们说另一个应用程序中的一个复杂视图是你的fatDogs视图。调用新的篮子视图fatDogsBasket。然后在代码中,在viewDidLoad中,只需对所有这些篮子执行以下操作...

Let's say one of your complicated views from the other app is your fatDogs view. Call the new basket view fatDogsBasket. Then in the code, in viewDidLoad, just do the following with all these "baskets"...

[fatDogsBasket addSubview:fatDogs.view];
[clientsBasket addSubview:clients.view];
[namesBasket addSubview:names.view];
[flashingLightsBasket addSubview:flashingLights.view];
// etc

你已经完成了! (显然,确保相关的视图控制器,fatDogs,flashingLights等都已准备好进行实例化。)

You're done! (Obviously, make sure that the relevant view controllers, fatDogs, flashingLights and so on, are all ready to go and instantiated.)

篮子系统非常方便每个人都会把你以前的工作放在一个地方;有用的是你可以(比方说)通过触摸篮子设置整体隐形或其他任何东西。显然,如果你想在代码中设置或移动篮子的位置,只需去

The "basket" system is handy since each one will hold your previous work in one place; usefully you can (say) set overall invisibility or whatever just by touching the baskets. Obviously, if you want to set, or maybe move, the position of a basket in the code, just go

happyBasket.frame = CGRectMake(509,413,
    happyBasket.frame.size.width,
    happyBasket.frame.size.height);

iOS中的UIViews非常轻量级,所以添加另一层UIViews完全没问题。

UIViews in iOS are very lightweight, so it's no problem at all adding another layer of UIViews.

我希望这是你得到的!

------稍后......

------Later...

你继续问:只是为了确保我能够以正确的方式实现这一点。主视图控制器为每个'篮子'提供IBOutlets和它与我正在调用的子视图的IBOutlet连接。我将在每个篮子中显示的每个视图控制器都有它自己的笔尖和相关的IBOutlets。对吧? -

You went on to ask: "Just to make sure I'm clear on the right way to implement this. The main view controller has IBOutlets for each of the 'baskets' and its this IBOutlet connection to the subview that I'm calling. Each of the view controllers that I'm going to show in each basket has it's own nib and associated IBOutlets. Right? –"

所以,主视图控制器为每个'篮子'都有IBOutlets...

So, "The main view controller has IBOutlets for each of the 'baskets'"...

对,新应用程序中的主视图,在.h文件中会有这样的行:

Right, the main view in the new app, would have lines like this in the .h file:

IBOutlet  UIView  *fatDogsBasket;

请注意,您只是将fastDogsBasket声明为UIView。你不应该过分担心IBOutlet这个词。所有这一切意味着我需要能够在界面控制器中查看此项目。重要的是要意识到它没有任何意义。

Note that you are simply declaring "fastDogsBasket" to be a UIView. You shouldn't worry too much about the "IBOutlet" word. All that means is "I need to be able to look this item up, over in the interface controller." It's important to realise IT DOES NOTHING.

所以,所有的篮子都是UIViews,因此你当然必须在你的.h文件中对它们进行delare。主视图控制器。就个人而言,不会使用视图控制器具有IBOutlets这一短语。它有点混乱,给出了错误的想法。只需说不要忘记在头文件中将UIViews标记为ibout。

So yes all the "baskets" will be UIViews and hence of course you must delare them as such in the .h file of your main view controller. Personally is would not use the phrase "a view controller has IBOutlets." It sort of confuses things and gives the wrong idea. Just say "don't forget to mark the UIViews as iboutlets in the header file."

所以无论如何是的,这正是你做的,宣布所有的篮子UIViews在主控制器的.h文件中,确实将它们全部标记为IBOutlets,以便界面构建器更容易工作。下一步..

So anyway yes that's exactly what you do, declare all the "basket" UIViews in the .h file of the main controller, and indeed mark them all as IBOutlets so that interface builder will work more easily. Next ..

它与我正在调用的子视图的IBOutlet连接 - 这是错误的。

"its this IBOutlet connection to the subview that I'm calling" -- that's wrong.

fatDogsBasket这样的篮子简直就是这样,就是这样。这只是一个UIView。

The basket such as fatDogsBasket IS SIMPLY A UIVIEW and that's that. It's just a UIView.

现在,如你所知,你可以把UIViews放在其他UIViews中。 (显然,这是司空见惯的,每个UIView都有很多UIViews,依此类推 - 这是构建界面最基本的部分。)

Now, as you know you can put UIViews inside other UIViews. (Obviously, this is commonplace, every UIView has scores of UIViews inside it and so on and on - it's the most basic part of building up an interface.)

所以,你打算在fatDogsBasket uiview中放什么?你将把所有你以前的工作放在fatDogs上!以前(对于iFone)你写了一个很棒的类 - 一个名为fatDogs的视图控制器。 (它甚至可能有很多子类等等。)

So, what are you going to put inside your fatDogsBasket uiview? You're going to put in ALL YOUR PREVIOUS WORK on fatDogs! Previously (for the iFone) you wrote a wonderful class - a view controller - called fatDogs. (It may well have even had many subclasses and so on.)

我们现在要从fatDogs中获取视图(当然,这是fatDogs.view)并且字面上把它放在fatDogsBasket里面。 (回想一下fatDogsBasket是一个UIView。)

We're now going to take the view from fatDogs (of course, that is fatDogs.view) and literally put it inside fatDogsBasket. (Recall that fatDogsBasket is a UIView.)

首先,你必须在新项目中完全包含你的惊人类fatDogs(来自旧项目)。点击添加现有的苍蝇/类或类似的东西......你会弄明白的。是的,添加所有类文件,xib,任何子类等。

So firstly you would have to completely include your amazing class fatDogs (from the old project) in your new project. Click "add existing flies/classes" or something like that...you'll figure it out. Yes, add all the class files, xibs, any subclasses and so on.

然后,只需在你的新超级强大的uber-controller中执行此操作,在viewDidLoad中,只需对所有篮子执行以下操作...

Then, simply do this .. in your new super-powerful uber-controller, in viewDidLoad, just do the following with all the "baskets"...

[fatDogsBasket addSubview:fatDogs.view];
[clientsBasket addSubview:clients.view];
[namesBasket addSubview:names.view];
[flashingLightsBasket addSubview:flashingLights.view];
// etc

你已经完成了!请注意,fatDogs(即fatDogs.view)中的视图现在显示在UIView fatDogsBasket内部。 fatDogs类现在可以完全正常工作,就像过去一样!令人难以置信的是,您可以轻松地(在您的新控制器中)执行简单移动fatDogsBasket之类的操作,并且它可以轻松地一次性移动fatDogs视图,而无需担心fatDogs及其视图的详细信息。

You're done! Note that the view from fatDogs (ie, fatDogs.view) is now displaying inside of the UIView fatDogsBasket. The class fatDogs will now work completely normally, just as it did in the old days! And incredibly, you can easily (here in your new controller) do things like simply move fatDogsBasket, and it will move the fatDogs view easily all at once, without worrying about the details of fatDogs and it's views.

最后你要问..

我将在每个篮子中显示的每个视图控制器都有自己的笔尖和关联IBOutlets。

"Each of the view controllers that I'm going to show in each basket has it's own nib and associated IBOutlets."

完全正确。当您将旧系统fatDogs添加到新项目时,您将添加所有的xib文件,依此类推。任何在这些类中发生或不发生的事情,与可能的按钮或任何其他标记为iboutlet或其他任何东西的事情一样,在这些类中仍然是相同的。我很确定当你在新项目中使用那些旧类时,绝对不会改变。

Exactly correct. When you add your old system "fatDogs" to the new project, you will be adding all of it's xib files and so on. Anyting that happens or doesn't happen inside those classes, to do with perhaps buttons or anything else marked as iboutlets, or anything else, will just still be the same within those classes. I'm pretty sure absolutely NOTHING will change when you use those old classes in your new project.

仅仅为了记录......我是每个视图控制器我要在每个篮子里展示..只是为了准确,你并没有真正显示为这样的viewcontroller,你展示了viewcontroller的视图(!!)。换句话说,对于fatDogs(一个视图控制器),你将显示它的视图,简单地说,它被称为fatDogs.view。 (view当然是任何UIViewController的属性,所以你可以说vcName.view并且你已经完成了。)

Just for the record .. "Each of the view controllers that I'm going to show in each basket.." Just to be accurate, you don't really show as such a viewcontroller, you show the view of the viewcontroller (!!). In other words, for fatDogs (a view controller) you will be showing it's view, which is, simply enough, referred to as fatDogs.view. ("view" is, of course, a property of any UIViewController, so you can just say vcName.view and you're done.)

希望它有所帮助!

最后你问................................ 。

And finally you ask .................................

我编译好了,但我的篮子显示为空,即他们没有显示我导入的视图控制器的视图。

"I've got it compiling OK, but my baskets are showing up empty, i.e. they're not showing the views of the view controllers that I've imported."


  1. Tell是来自旧项目的一个UIViewController类的名称,所以我们可以是具体的

  1. Tell is the name of one of your UIViewController classes from the old project, so we can be specific

假设您有一个名为HappyThing的旧UIViewController。所以你很可能有一个文件HappyThing.h和一个文件HappyThing.m和一个文件HappyThing.xib。

Let's say you have an old UIViewController called HappyThing. So you will very likely have a file HappyThing.h and a file HappyThing.m and a file HappyThing.xib.

把所有这些放在新项目中,你必须使用Add-> Existing Files这样做。 (控制XCode左侧列表中的一个当前文件名。)

put all those in the new project, you must do so using Add->Existing Files. (Control on one of your current filenames in the list on the left in XCode.)

你必须在某处执行#importHappyThing.h您的新项目中的其他项目 - 在Prefix.pch文件中或在新的UIControllerView的顶部

You will have to do this #import "HappyThing.h" somewhere or other in your new project - either in your Prefix.pch file or at the top of your new UIControllerView

要在HappyThing.h中清楚,您将拥有一行代码
@interface HappyThing:UIViewController

To be clear in HappyThing.h you will have a line of code @interface HappyThing : UIViewController

在新的UIViewController.h文件中,你必须添加一个新变量,我们'将其称为xxx,

In your new UIViewController.h file, you will have to add a new variable, we'll call it xxx,

HappyThing * xxx;

HappyThing *xxx;

请注意,xxx的类型是HappyThing。 (请注意,作为一项规则,您将使用命名约定happyThing(注意小写h)而不是xxx,但它只是一个变量,我希望它清楚地表明它只是一个变量。 )

Note that the type of xxx is HappyThing. (Note that as a rule, you would use the naming convention "happyThing" (note the lowercase "h") rather than "xxx", but it's just a variable and I want it to be clear to you that it's just a variable.)


  1. 下一个!目前它只是一个没有指向任何东西的变量,它什么都没有。 (就像你说int x,但实际上并没有说x = 3或其他什么。)所以!在您的代码中,您必须实际实例化xxx。

  1. Next! At the moment it's just a variable that is not pointing to anything, it's nothing. (Just as if you said "int x", but then did not actually say "x = 3" or whatever.) So! In your code you have to actually instantiate xxx.

xxx = [[HappyThing alloc] init];
[xxxBasket addSubview:xxx.view];

xxx = [[HappyThing alloc] init]; [xxxBasket addSubview:xxx.view];

注意第一行是实例的原因HappyThing的存在。当然,您希望使用xxx指向该实例。

Note that the first line is what makes an instance of HappyThing come in to existence. And of course, you want to use "xxx" to point to that instance.

第二行将视图放入相关的篮子中!请注意,当然你想要的是与xxx相关的视图(即xxx.view)...记住xxx是一个UIViewController,它本身不是UIView。关联的UIView是xxx.view。 (该视图实际上只是xxx的属性。)

The second line puts the view in to the relevant basket! Note that of course what you want is the view associated with xxx (ie, xxx.view) ... remember that xxx is a UIViewController, it is not itself a UIView. The associated UIView is "xxx.view". (The view is literally just a property of xxx.)


  1. 内存管理!请注意,您使用alloc来使xxx存在。这意味着你拥有它,当然这意味着你不需要在那里发送保留。此外,既然你拥有它,那意味着你最终必须释放它。 (简单... [xxx发布];)

所以只需添加行[xxx release];到新的UIViewController中的dealloc例程。 (如果你忘记这样做,它确实不会造成任何伤害,但无论如何都要这样做。)可以想象,一旦你对这个过程更加满意,你可能会因某种原因提前释放它。

So simply add the line [xxx release]; to the dealloc routine in your new UIViewController. (Really it won't cause any harm if you forget to do this, but do it anyway.) Conceivably you may want to release it earlier for some reason once you are more comfortable with the process.

(我当时正在研究一个拥有大量巨型桌子,弹出窗口等的项目,所以我只是在飞行中制作它们并尽快摆脱它们,以减少使用内存但是在这个阶段所有这一切与你无关。)

(I was just working on a project with a huge number of huge tables, popovers and the like, so I only made them on the fly and got rid of them as soon as possible, to use less memory. But all of that is irrelevant to you at this stage.)

现在你应该在屏幕上看到它!

So now you should SEE IT ON THE SCREEN!

不要忘记,如果你以前在HappyThing中有一些例程,你必须调用它来启动它(可能是beginProcessing或其他东西),你必须自己从新的UIViewController调用它。因此可能类似于:

Don't forget if you previously had some routine in HappyThing, which you had to call to start it working (perhaps "beginProcessing" or something), you'll have to call that yourself from the new UIViewController. Hence perhaps something like:

xxx = [[HappyThing alloc] init];
[xxxBasket addSubview:xxx.view];
[xxx beginProcessing];
[xxx showAmazingRedFlashingLights]; // or whatever

最后你问...

当你使用这种技术时,你只是在主视图控制器中包含导入文件的标题,还是以某种方式转发它们?

"When you've use this technique, do you simply include the headers of the imported files in your main view controller, or do you forward class them in some way?"

这不是你的问题,你的问题是你没有使用xxx = [[HappyThing alloc] init];行来实例化它。祝你好运!

That was not your problem, your problem was that you were not instantiating it with the line xxx = [[HappyThing alloc] init];. So, good luck!

关于代码行@class HapyyThing,如果你想简单地把它放在新UIControllerView定义的开头上面。一般情况下,如果您将包含线放在最佳位置,则不必。无论如何,这是一个无关的问题。如果您的新UIViewController无法编译,则无法找到HappyThing。享受!

Regarding the line of code "@class HapyyThing", if you want to simply put it just above the start of the definition of your new UIControllerView. Generally you don't have to if you have your include line in the best place. Anyway it is an unrelated issue. It simply won't compile if your new UIViewController, can't find HappyThing. Enjoy!

这篇关于管理iPad端口中的View Controllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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