How do I make an action in one view close itself and open another view? - objective-c

New to obj-c and cocoa, working a on a simple game as my first (mac os x) app. I have a menu view and a game view:
MenuView.h/m
MenuViewController.h/m
GameView.h/m
GameViewController.h/m
I want the menu to be displayed by default, and when the play button (which is in the MenuView) is clicked I want the menu to go away and the game to appear. I understand actions and outlets, but I don't know where to start on making the views swap themselves out. Any help? It seems like I would need to somehow make my MenuViewController talk to my MainController?

Peter Hosey,
If you are looking at the MenuViewController as a sort of "main" view controller, at the moment the play button is clicked, you could initialize an instance of GameViewController:
- (IBAction)gameButtonClicked {
GameViewController *game = [[GameViewController alloc] init];
// we put the game view as the MenuViewController's view
[self setView:game.view];
[game release];
}
Of course, you need to import the "GameViewController.h" to access it.

If you want them in the same window and at the same size, you can put both views into a tabless tab view. Just switch the tab to switch which view is visible.
Another way would be to put them in different windows and use a window controller rather than a view controller for each one. Among other things, this makes it easy to make the game window user-resizable without disturbing the size of the menu window.

Since you want your MenuViewController to be what calls and takes care of GameViewController, I'd suggest something similar to what Rafael said. Try putting an instance of GameViewController in your Interface Builder file, but making it hidden. When they press the "Play" button to activate the game, simply send the GameViewController to the front and make it visible. It's a bit clumsy, but it works. Remember to hide all the buttons and interactions regarding your menu though. You don't want to accidentally activate the high scores list when playing a game!
Also, I'd recommend looking into Utility applications. (They're one of the template types you're given when you create a new project.) They're built to switch between two seperate view controllers, and it might just be what you're looking for.

Related

Two view controllers (with nibs) acting on the screen at the same time

Just as a disclaimer, I am an iOS beginner, in a pretty unique position. I'm an intern learning on the job, with a hard deadline for a test app, with specific specs. So I unfortunately don't have the luxury of learning all that I should about xCode and objective C.
Anyways, I am trying to create a navigation menu similar to the iPad's slide out menu. I've looked at plenty of sample code given in response to questions like this in the past. It all works perfectly fine, but I can't understand all of what they're doing. I think this results from being fairly bewildered by view controllers and delegates. And, since I'm supposed to be writing this all by myself, I can't just build off of their existing code.
My plan for this working is to have one main view controller, containing the navigation menu in a table view. This is hidden behind a normal UIView, until a button is pressed, at which point the normal UIView slides offscreen enough to reveal the menu. Upon selection of a menu item, the normal UIView would slide back to its original position, and be replaced by the relevant UIView, controlled by its view controller. In other words, clicking on the menu item relating to "Home" would load the homeViewController.xib, controlled by the homeViewController. This would be loaded in that normal UIView subview, on top of the tableView.
I'm able to load a view in that normal UIView as a result of a button press.
homeViewController *_homeViewController = [[homeViewController alloc]initWithNibName:#"homeViewController"];
[self frontView] = _homeViewController.view;
There may be some syntax errors in that code, since its from memory. I'm not able to access the computer with my code on it at the moment, but that's the general gist. Basically, that places the home view nib on the frontView (that normal UIView that's over the table view), but its not connected to homeViewController. Or at least I think that's the issue, when I press a button, that's correctly connected between the nib and the homeViewController.h, and only set to NSLog in the .m file, the application crashes.
Any idea how to connect this nib to its viewController when displayed in this way? Or, to create the nib in IB, without the associated .h and .m files, and use the main view controller as the homeViewController as well? Or, if my logic is inherently flawed, what SHOULD I do? Something with NavigationControllers?
EDIT:
I also tried a new approach- changing homeViewController's file owner to viewController, and connecting the button on homeViewController's action to viewController. This too caused a crash upon pressing the button. I really think the issue is with having multiple view controllers acting on screen at once, or having multiple views from separate nibs, controlled by one view controller on screen at once.
I suspect that your immediate problem is that _homeViewController is being freed as soon as you leave whatever method that code is in. To fix this, create a strong property in this class that holds the _homeViewController for as long as its view is needed and allocate it to that property rather than a local variable. That way, buttons (or whatever) that are part of that view still have a valid controller object backing them.
In the long run, pushing or presenting view controllers when you need the screen to change is a much better strategy but, as I said, the immediate problem.... :)

Manipulate UI from AppDelegate

I am a newbie in all this as will be apparent really soon.
I am using the iOS: Application: Tabbed application template. I have placed a UIImageView in the first view and two standard rounded buttons. One button is attached to an action in the FirstView Controller which places a picture into the UIImageView. The second button is attached to an action in the AppDelegate which calls a method in the FirstViewController which in turn places a second picture into the UIImageView.
The AppDelegate method does not replace the picture. It doesn't crash… it just does not seem to do anything.
How can I manipulate the view in the First and Second View Controller from the AppDelegate?
#dasdom
Well that's one issue explained. I've been reading the theory of MVC and trying to put it into practice now. Short version is I am trying to write a Battleship app for practice. Was planning on using the first screen to setup the game pieces, prefs, etc.. and use the second screen for actual game play.
I've created another class to use as my "brain center" but I ran into the same issue of not being able to manipulate anything on the screen for the first or second views. (That's why I tried the appDelegate).
That's my life story right now… can you throw some pointers my way on how to proceed and how to solve my one of many problems?
First you shouldn't do that. The AppDelegate should only be responsible for bringing the first view onto the screen.
Second you should have a look into the Model-View-Controller design pattern. Search for it in your preferred search machine.
But I you really still want to do that you should have a look into delegation and/or notifications. For example you could send the First View Controller a notification from the AppDelegate to change the image.

IOS menu/design

Relevant Data:
I'm making a simple game, using OpenglES. The game itself is done, however I would like to have a main menu as well as some other screens designed in IB. So far I have a death/score screen that is displayed with a simple modelviewcontroller.
I haven't done a ton with GUI building or much programming on the platform outside of C code (posix sockets) and some examples from some books. So I'm not sure how I would go about having lots of views- usually I just use a model view, and it's gotten me along just fine so far. However I don't think that would be the best route here.
Situation:
I have a view controller that shows my main menu- the main menu branches off to the main game, a settings screen, and a high score screen. The main game is made in opengl, and I haven't made the settings view yet, but it likely will be as well. How should I switch between the views? Presenting the main view from the app delegate is as simple as setting the root view controller = newly created view controller, should I do the same thing here? If I do that can I remove the resources from the menu view controller?
Sorry if this is an extremely simple question, I just don't quite get the process.
I'm not entirely sure what you want to do, but an easy way to show a new view controller is:
SecondViewController *aSecondViewController = [[SecondViewController alloc]
initWithNibName:#"SecondView" bundle:nil];
[self presentModalViewController:aSecondViewController animated:YES];
I hope that helps.
How should I switch between the views?
In the vast majority of cases, you should be using a UINavigationController. Your initial controller would be the main menu. When you want to go into a particular section of your application, you push a new view controller onto the stack. When you want to come back out, you pop it off the stack.
Besides navigation and presenting modally that others have mentioned, another option is to swap out views. May or may not fit your app's flow but wanted to point out another option for you to consider
Best practice for switching iPhone views?
If you are already limiting the game to iOS 5 for some other reason you should look into UIStoryboard. If you don't currently require iOS5 the "simplest" way is to use table views, but that isn't a very "gamey" UI.

what am I doing wrong in attempt to popup a web view and then allow user to go back

my app has tabBarController with 3 views and in one of them I want to popup a web browser with the ability to return back to the application. To do that I am using UINavigationController.
In myAppDelegate.h I have defined the property UINavigationController *nav and in myAppDelegate.m I have #synthesize nav.
In the class where the webPopup function resides upon pressing the button my code comes to this function.
- (IBAction)showWeb:(id)sender {
myAppDelegate *app=[[UINavigationController alloc] initWIthRootViewController:self];
// because I want to return back to the same view
webController *web = [[webController alloc] initWithStyle:UITableViewStypeGrouped];
[app.nav pushViewController:web animated:YES];
app.nav.view.frame = CGRect(,0,320,430);
[self.view.window addSUbview:app.nav.view];
}
The web popup occurs but it is moved vertically, when I press "back button" my former view appears as well and it is also shifted vertically from what it was before.
After going back and forth few times the thing hangs.
Questions:
1. what can cause the shift?
2. how to avoid when I go "back" to see the title(test from the "back"button, I think this might cause a shift when I go back.
3. how to find out why it hangs after few attempt?
Thanks.
Victor
The line:
myAppDelegate *app=[[UINavigationController alloc] initWIthRootViewController:self];
makes no sense to me. Surely your compiler is warning you about that? What is "myAppDelegate" defined as? Classes should have a capital letter at the front, by the way.
Also, the line
[self.view.window addSUbview:app.nav.view];
is highly suspect, because the UIWindow for your application should have only one child UIView. You shouldn't just add more views willy nilly and expect things to work. It is possible to change the child UIView by removing the old one and adding a new one, but you don't seem to be doing that. Having more than one child UIView of UIWindow gets you into trouble very quickly -- for example, device orientation changing can break.
I'm not exactly clear as to why the app delegate (or the window for that matter) needs to be messed with at all to do what you are trying to do. Sounds like you should just be using standard Nav View Controllers and Web Views.
Also, you are alloc init'ing w/o any memory management.

Can I use UITabBarController as a simple viewController switcher?

I'm creating an iPad app based on a UINavigationController (with the bar hidden) so I can push and pop other viewControllers for navigation around the app. However, I am now wanting to add a section in which there are two viewControllers that I want to be able to switch between, so in other words they are side-by-side, rather than hierarchical.
Is it okay to use a UITabBarController for this? I am aware that on the iPhone it is recommended they are used only at the root level of the app, but since this is an iPad app I wondered if I could use it? Also, I guess I need to create an empty viewController, create a UITabBarController within it and set the delegate to it, then add the two viewControllers to it... So in effect I will have a viewController within another viewController, and when I have done that in the past the results have been very flaky.
Can I do it this way? The only other way I can think of doing it is to have two plan UIViews within a UIViewController, but that also means I shouldn't really put any business logic in them (bad MVC!), and not being able to will be a right pain in the a**.
Any ideas?
Thanks!
:-Joe
EDIT: I also need to be able to swipe-animate between the two VCs within the TabBarController, AND have a menubar over the top which doesn't animate... Can I do this?
Sure.
I do this kind of thing all over the place in an app I'm working on. I actually have several different types of "toolbars" that can be optionally shown at different times.
What I do is create a "parent" member in my toolbar's class - and when a button is pressed, I have the toolbar call a method in the parent class to do whatever needs to be done - (i.e. display another view).
This avoids the whole mess of creating a view inside another view (or viewcontroller inside another viewcontroller - or whatever) - the toolbar can take the button hits, but all the views are opened by the root view/controller.
Hope this helps/makes sense!