Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
I still need to have a controller for View2, don't I?
Not necessarily. A viewController that handles both View1 and your "pop-up" View2 should be sufficient.

I just return the view after I have loaded the View2Controller and don't care about the controller anymore, and use View1Controller as a delegate for View2, correct?
That last bit sounds correct but how are you loading View2? You don't need to load it via a viewController.

P.S. Since your main, game viewController is going to be handling things for View1 and View2 don't name it View1Controller. That's just confusing. I'd call it something like MainViewController.
 
It's easy to feel totally dumb...

When reading the " View Controller Programming Guide...", it seems to always be assumed that the views are having it's own view controller. Well, it is actually called "View Controller Programming...".

This is how I load View2

In View1Controller.h I define

Code:
 View2Controller *view2;

Somewhere in View1Controller.m

Code:
view2 = [[View2Controller alloc] initWithNibName:@"View2" bundle:nil];
[self.view addSubview:view2.view];

Hence, a controller is needed in that way.

View2 is created in IB so in some way I need to either load it or connect it to a IBOutlet (I guess). But I don't even know what specifier to use for View2... Do I have to create my own custom view (inheriting UIView), programatically, and associate it with a view in IB in some way, or...?

In general, does it make any different loading the view or using IBOutlets and connecting it to the view? Memory wise it must be better to load the views when needing them, but how about functionality?

When on to it, using a modalViewController is not an option when not using view controllers, is it? Once again because the above mentioned guide is only talking about how to do this using controllers.

Thanks for you patience!
 
Somewhere in View1Controller.m

Code:
view2 = [[View2Controller alloc] initWithNibName:@"View2" bundle:nil];
[self.view addSubview:view2.view];

Hence, a controller is needed in that way.
There's another way to load a NIB, one that doesn't require initiating view controller, using NSBundle's loadNibNamed: owner: options: instance method.

You can take a peek at the Resource Programming Guide, in particular the Nib Files section, for more info.
 
Additional to above, I tried this as well.

1. Creating a new view in IB (Saved in ViewWithoutOwnController.xib/nib).
2. Creating a new class in XCode called ViewWithoutOwnController and inherits from UIView.
3. In View1Controller.h, I did create an IBOutlet like this:

Code:
#import "ViewWithoutOwnController.h"
//Other imports and @class definitions...

@class ViewWithoutOwnController;

@interface View1Controller : UIViewController {
/*
Other declarations...
*/
	IBOutlet ViewWithoutOwnController *viewNoCtr;

}

/*
Other declarations...
*/

@property (nonatomic, retain) IBOutlet ViewWithoutOwnController *viewNoCtr;

@end

4. In IB, I draged a generic object to the document window of View1 (File's Owner is View1Controller) and associated it with the ViewWithoutOwnController class. Then I linked that object to the outlet defined in File's Owner. Now the outlet should be defined in class View1Controller I assume. Note: I also tried this step, defining the File's Owner in the document window of ViewWithoutOwnController to View1Controller, and making the link to the outlet there instead (removing the object in View1's document window first). No difference in behaviour of the application.

5. In View1Controller, I tried presenting the new view like this:

Code:
@synthesize viewNoCtr; //After @implementation, not inside touchesEnded

[self.view addSubview:viewNoCtr];  //Inside touchesEnded

I just changed the line where I used to present view2 as defined above. Now nothing happens when I try to add that new viewNoCtr, not even an error...

P.S. Noticed I got a new message while writing this post... D.S.
 
YES, finally, things are starting to work as expected. Thanks so much for all help so far.

I am now succesfully loading my views like this:

View1Controller.h:

Code:
//imports...

@interface View1Controller : UIViewController {

	IBOutlet UIView *view2;
	IBOutlet View1 *view1;
}

@property (nonatomic, retain) IBOutlet UIView *view2;
@property (nonatomic, retain) IBOutlet View1 *view1;

loadView in View1Controller.m

Code:
-(void) loadView{
	
	[[NSBundle mainBundle] loadNibNamed:@"View1" owner:self options:nil];
	[[NSBundle mainBundle] loadNibNamed:@"View2" owner:self options:nil];
	
	self.view = view1;
	
}

And I can add and remove them as expected.

I will put all significant code here once I have solved my, most likely final, question for getting this structure/skeleton to work.

My last nagging problem: I cannot access methods in View1 in a good way. I want to access some methods in View1 when there is no risk that timing will be a problem (I think that's the problem I sometimes get when posting a request to the View1's method). When I add the request in touchesEnded of View1Controller just before or after when I add subview View2, sometimes the application crashes, sometimes the method actually is reached. Where should I put the request in order to make sure it is not requested while View2 is added or removed? viewWillAppear and viewDidAppear seems to be not called, which is strange. According to this section of "View Controller Programming Guide", on page 46, it should be access when the view is presented, or just before for viewWillAppear. OR...when the View2 is removed according to the paranthesis below...:

Figure 2-9 shows the basic sequence of events that occurs when a view controller’s view is added to a window. (If the view is already in a window but currently hidden by another view, this same sequence of events occurs when those obstructions are removed and the view is once again revealed.) The viewWillAppear: and viewDidAppear: methods give subclasses a chance to perform any additional actions related to the appearance of the view.

touchesEnded in View1Controller.m

Code:
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{
	
	UITouch *touch = [touches anyObject];
	CGPoint point = [touch locationInView:[touch view]];
	xHit = (int)(point.x);
	yHit = (int)(point.y);
	
	[B][self.view addSubview:view2];[/B]

}

and the IBAction that is linked to TouchUpInside of the UIButton in View2:

Code:
-(IBAction) removeView2:(id)sender{
		[B][view2 removeFromSuperview];[/B]
}


Any suggestions, please?

Thanks again!
 
My last nagging problem: I cannot access methods in View1 in a good way. I want to access some methods in View1 when there is not risk that timing will be a problem (I think that's the problem I sometimes get when posting a request to the View1's method). When I add the request in touchesEnded of View1Controller just before or after when I add subview View2, sometimes the application crashes, sometimes the method actually is reached. Where should I put the request in order to make sure it is not requested while View2 is added or removed?
I think I may need a bit more background on the purpose of these (View1) methods that you are trying to access before I can answer your question. What is their function and how does calling them affect the rest of the application? Maybe step me through a typical UI interaction scenario, if that makes sense. Once I get a better handle on what they do, I may ask for more code on their definitions and how you are calling them currently.
 
To be consisten with above, I am not changing the names of the classes/views even if that would make sense to do now...

Ok, here's how it goes:

1. ForumViewController is presented when the app starts. The user choses "Play Game"

2. The View1Controller with CustomeView1 is loaded into property view1 and presented. As mentioned above, View2 is also loaded and allocated to property view2. This is done in loadView of View1Controller. This part is presente above in my earlier post.

View1 is a gameboard with brics. View2 is the heads-up display where the user can chose the direction to move some brics.

3. View1 is filled with brics. The user choses one of the brics. touchesEended catches this touch, stores the x- and y-coordinates in instance variables. Then adds view2 by doing this in touchesEnded: [self.view addSubview:view2];

4. View2 appears. The user choses one of the buttons. Perhaps button pointing up because the user wants to move the brics in that direction.

5. The TouchUpInside of the UIButton is connected to an IBAction (removeView2) of View1Controller. In removeView2 i remove view2 by doing this: [view2 removeFromSuperview]; In removeView2 I save the choice by using the [sender tag] property (predefined differently for each button).

Here (or inside touchesEnded or viewWill/DidAppear or ?), what I want to do is to call a method in view1 (class CustomView1). I want to send the x- and y-coordinates to that class because I use them when updating view1, hiding brics and moving brics around. These things I do in CustomView1.

When googling around, I find that when adding subviews, viewWill/DidAppear is not triggered inte the view controller but must be called from the subviews using super. But since the viewWill/DidAppear isn't even triggered in the subviews, it's not possible. Also, in the View2, I don't have any custom class, I only load the view from the nib file and it has no association with any class in XCode, so couldn't trigger anything there anyway. Perhaps it's not needed anyway just for view2.

What's the cure?

Can there be any timing issues that when I try to access the method of view1, the application is still adding or removing view2?

Thanks!
 
5. The TouchUpInside of the UIButton is connected to an IBAction (removeView2) of View1Controller. In removeView2 i remove view2 by doing this: [view2 removeFromSuperview]; In removeView2 I save the choice by using the [sender tag] property (predefined differently for each button).
To what are you saving the choice? An instance variable in View1Controller?

Here (or inside touchesEnded or viewWill/DidAppear or ?), what I want to do is to call a method in view1 (class CustomView1). I want to send the x- and y-coordinates to that class because I use them when updating view1, hiding brics and moving brics around. These things I do in CustomView1.
Well, first, realize that viewWill/DidAppear are methods of a viewController and not of a view.

Second, I would suggest you don't have View1 responsible for hiding/moving brics; have a method in View1Controller do that. Then simply call a method in View1 to update its view. If you insist to do it your suggested way, it is possible; you just need to make sure that View2 can reference View1 (like by adding a View1 property to View2).

Third, are you sure you want to popup a menu to control the bric movement? How about using swipes on the View1 itself?
 
Quote:
Originally Posted by Nicsoft View Post
5. The TouchUpInside of the UIButton is connected to an IBAction (removeView2) of View1Controller. In removeView2 i remove view2 by doing this: [view2 removeFromSuperview]; In removeView2 I save the choice by using the [sender tag] property (predefined differently for each button).
To what are you saving the choice? An instance variable in View1Controller?

An instance variable I'm supposed to. Haven't done it yet, haven't consider this to be a problem since it feels quite straight forward how to do this saving kind of activity. May I ask what thoughts you had that made you ask this?


Quote:
Originally Posted by Nicsoft View Post
Here (or inside touchesEnded or viewWill/DidAppear or ?), what I want to do is to call a method in view1 (class CustomView1). I want to send the x- and y-coordinates to that class because I use them when updating view1, hiding brics and moving brics around. These things I do in CustomView1.
Well, first, realize that viewWill/DidAppear are methods of a viewController and not of a view.

I do define this method in my View1Controller (where the views are added and removed), never ever triggered... One note: it is triggered in ForumViewController when I start the application. I made a test, removing view1 (removeFromSuperview) from this View1Controller, but the viewDidAppear in ForumViewController is not triggered when presented again. Don't know if it matters, but I do load View1Controller from ForumViewController by doing "[self.view addSubview:view1.view];". It's not a problem that I add it as a subview, is it?

Second, I would suggest you don't have View1 responsible for hiding/moving brics; have a method in View1Controller do that. Then simply call a method in View1 to update its view.

I do the processing of how the screen should look like at next presentation in view1. Moving this logic to the controller, will it help me in this problem, or is it just for creating a better design? I do the "[self.view setNeedsDisplay];" in the controller already, in the removeView2.

If you insist to do it your suggested way

I do not... :)

Third, are you sure you want to popup a menu to control the bric movement? How about using swipes on the View1 itself?

Yes, unfortunately this is much better from usability point of view. I did consider this solution though, but the interaction is not as simple as just "moving one bric"...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.