So, this is my question. I have a ViewController, and I want to add 2 different Views in different position (one upper and the other lower) but they will appear at the same time, using method "addSubview", but I don't know if this is correct, to have different subViews in one Viewcontroler?
I'm not using Storyboards, I'm using XCode4.1 to iOs 4.3
Asked
Active
Viewed 83 times
0

user1600801
- 269
- 7
- 25
1 Answers
0
You mix up views and view controllers.
The number of views that a view controller can control to display its data and perform its business logic is not strictly limited.
Nor is the number of subviews limited that a view can have.
There is just one thing. When these views are rather independent from each other, then do not make one a subview of the other. If a is the underlying view and b and c are the indipendent subviews, then do: [a addSubView:b]; [a addSubView:c];
Where is the problem?

Hermann Klecker
- 14,039
- 5
- 48
- 71
-
Thank you for your time. This is kind off my situation. I have ViewController A and in this ViewController I have declared UIViewController "x", and UIViewController "y". Now, in the viewDidLoad method from "A", and doing this: "[A addSubView:x.view]; [A addSubview:y.view]". So, is this right? – user1600801 Mar 11 '13 at 23:36
-
NO, that is not right. If x and y are view controller (objects of a class that is or inherits from UIViewController) then you cannot add them using A addSubView: . And you cannot use addSubView on a if A is a view controller. You can only add subviews to views (Objects inheriting from UIView). And all of them can be controlled by the same view controller. – Hermann Klecker Mar 11 '13 at 23:41
-
Just to add some complexity to a simple thing: You could create a number of view controllers with their related views and add those view controllers to a view that is controlled by another view controller. That is very rarely a smart thing to do. But it can be done. As you are on SDK 4.x, you cannot make use of the UIContainerView which was introduced with 6.x (I think) for exactly this purpose. – Hermann Klecker Mar 11 '13 at 23:45
-
So, if A have a UIView, and "x", and "y" also have a UIView, can I add [A.view addSubView:x.view] and [A.view addSubView:y.view]"? Is that correct? – user1600801 Mar 11 '13 at 23:46
-
Just do it if you need to do it. But please ask yourself if you really need to display several view controllers at the same time. If you do then do it. In the end they are objective-c objects like others, can be created and used and referred to and everything. – Hermann Klecker Mar 11 '13 at 23:48
-
The problem is I really need to display several view controllers at the same time... At least, 3... – user1600801 Mar 11 '13 at 23:49
-
But now, how this Views can interact/communicate? I mean, how the subView "x" can call a method in the subView "y"? – user1600801 Mar 12 '13 at 00:01
-
Views that belong to several view controllers should not communicate with each other. But their controllers could. The proper way is the delegate pattern. But you could just let a.myB = b; and b.myA = a; some time upon initialization so that they know each other and can call each others public methods. – Hermann Klecker Mar 12 '13 at 00:15
-
I just doubt that you really need this number of view CONTROLLERS. And I fear that you are about to get lost between them. – Hermann Klecker Mar 12 '13 at 00:18
-
I hope not... This is the only solution that came to my mind. This is what I have/need. I have a ViewController, in which I switch its views (I display a Table or I display some graphics). I need to add something like a ToolBar upper this view.I already have the UINavigationController toolbar, but I could not add another Toolbar, so this is why I want to add another View, like the ToolBar I need to add.. – user1600801 Mar 12 '13 at 00:23
-
Year, that is what I meant. You can add ToolBars and TabBars etc into a single view. Those are -so far- additional views only. All of them can be managed by the same view controller. However, with a UITable it gets difficult. The UITableView controller expects its very own table exactly at self.view. Otherwise it will throw an exception at runtime. Frankly I do not know out of my head how to get along with that (with one view controller only) in iOS 4.x. – Hermann Klecker Mar 12 '13 at 08:30
-
Ok, thank you very much... I hope not to get exception's at runtime – user1600801 Mar 12 '13 at 23:17