new view for menu for simple ios word game - objective-c

I'm making a simple word game in Objective-C for iOS, and I started out by selecting "Single View Application", so I have one myProgramViewController.xib file, and one each of myProgramViewController.h and myProgramViewController.m. I just now realized that I would like to have a main menu type thing that loads first, and allows the user to select the length of the word, difficulty, stuff like that, and a "Go!" or "Start!" button. How can I add this menu view? I would prefer that the user could not go back to it (for now), so I'd rather not use the navigation bar.

Context in comments above.
When a segue happens, you can pass any data from one ViewController to another. What I would do in your case is set up with a storyboard, have the menu controller be the initial view controller, then have the game screen be a modal segue from that.
In the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender (on your MenuViewController) you can get segue.destinationViewController and set the data on it.
Example:
MenuViewController ---- playGameSegue ----> GameViewController
In MenuViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
GameViewController * destination = (GameViewController*) sender.destinationViewController;
destination.anyPropertyYou'veDefinedInGameViewController.h = DATA;
destination.otherProperty = DATA;
}

You can present the view modally. Say you have a view called MainMenu, which has a .xib,.h and .m file.
In your myProgramViewController.m file, you can call the code
MainMenu *mainMenu = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self presentModalViewController:mainMenu animated:YES];
Then in your MainMenu.m file, once the user has made their selections, call:
[self dismissModalViewControllerAnimated:YES];

Related

Cannot have the father uiViewController with the back item?

I have seen this topic (iOS Storyboard Back Button) and more about this subject, but I still cannot have my back button appear on screen :
I have got 2 viewControllers, the two of them have a navigationController, the "father" controller has the button bar item set to "back" as a plain text, the second viewController appear well with a modal segue, or with "show detail (replace)" segue, but nothing appear on the navigation bar to come back... Would you know why?
Here is a capture :
Thanks
EDIT :
with a custom transition, and when presenting the controller via the navigator in the code, the back button is not here anymore... would someone know why?
When I comment out presentViewController:secondViewController, the back button is here, but the custom animation is not triggered anymore, there is the normal transition set in the storyboard.
Here is my method :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"segue descr %# : ", [[sender class] description] );
if ( [[segue identifier] isEqualToString:#"second"] ){
//SecondViewController *secondViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"SecondViewController"];
SecondViewController *secondViewController = [segue destinationViewController];
secondViewController.transitioningDelegate = self;
[self.navigationController presentViewController:secondViewController
animated:YES
completion:nil];
Modal and show detail segues don't use a back button, because they are like independent views.
The idea is that those those views are only showing extra information or details about something and you can close them programmatically when you need to go back to the previous view.
A show or push segue will give you the back button in your navigation controller, because that segue is meant to be more like a sequence of views.
When you push a View Controller, you get the back button for "free" without having to write extra code. When you present a modal View Controller, you need to add your own way of dismissing the modal view. Since you have a Navigation Controller, your easiest route is probably to add a UIBarButtonItem to the navigation bar, and have that bar button call dismissViewControllerAnimated:completion: in your UIViewController subclass.

prepareForSegue not being called from UITableView in a UIViewController

I have a UIViewController that contains a UITableView. The table view contains a custom UITableViewCell. The custom cell was built in interface builder and has a nib. In my main storyboard, I dragged a segue from the custom table view cell to the destination view controller. I set up the bare bones essentials in prepareForSegue, set a break point, but it never gets called.
I'm not that accustomed to using a UITableView in a view controller. I usually use a UITableViewController, but requirements dictate using the table view in a view controller. My initial assumptions is that most methods of doing things would be nearly identical, but I'm finding that not to be the case.
I tried setting the segue from the view controller itself and using didSelectRowAtIndexPath, and though it worked, the transition to the destination view controller was jerky.
Can anyone suggest what I might be missing in order to cause the prepareForSegue method to fire?
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
GaugeViewController *destination = [segue destinationViewController];
[destination setGaugeID:#"1"];
}
Thanks!
You need to refer to the identity of the segue in the Storyboard, something like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
GaugeViewController *destination = segue.destinationViewController;
if([segue.identifier isEqualToString:#"yourSegue"]) {
NSLog(#"prepareForSegue called");
[destination setGaugeID:#"1"];
}else {
// do something else
}
}
Also don't forget to set the Identifier in the Storyboard.
Remember that push segues are used with Navigation Controllers and a modal segue can be dragged from view controller to view controller.

Pass data between storyboards, not master detail template

Is it possible to pass data between storyboards in objective-c with segue when the application is not made from a master detail template?
The only examples I have seen is the one with master detail views.
Yes, it is. You could implement prepareForSegue:sender: in the viewController you are segueing from, like so:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"MySegue"]) {
MyOtherViewController *destination = (MyOtherViewController *)segue.destinationViewController;
destination.someProperty = self.someOtherProperty;
}
}
This will get called before your segue is performed, giving you a hook into the viewController you are segueing to.
Edit:
I didn't realise your ViewControllers were in different Storyboard. This question has already been pretty much answered, here: https://stackoverflow.com/a/9610972/1716763
You wouldn't actually be using a segue though, you would do things programatically and either push your second view controller onto the navigation stack, or present it modally.
I've adapted some of the code from #Inafziger's answer to fit your example:
UIStoryboard *secondStoryBoard = [UIStoryboard storyboardWithName:#"secondStoryBoard" bundle:nil];
MyOtherViewController *myViewController = (MyOtherViewController *)[secondStoryBoard instantiateViewControllerWithIdentifier:#"myOtherViewController"];
myViewController.someProperty = self.someOtherProperty;
[self.navigationController pushViewController:myViewController animated:YES];

How to presentModalViewController without dismiss the TabBarController

Hey guys i`m trying to present a modal view controller inside an application with a tab bar controller. The problem is, every time the new view is presented, it on top of my tab bar.
I need to keep my tab bar even when the view is presented. Like google maps application does with his toolbar at the bottom of the screen.
How can i do that?
Thank you
By default, a modal view controller is meant to take up the entire screen (on an iPhone/iPod, at least). Because of this, it covers whatever you have on screen at the time.
A view controller presented via modal segue is meant to live on its own. If you want to keep your Navigation and TabBar, then just use a push segue to present the new ViewController. Remember to use this kind of segue, your presenting controller needs to be part of a UINavigationController already.
Use this to push a ViewController. If it is a UINavigationController it will push its linked RootViewController by itsself.
Create a viewController to push: (Using Storyboard)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
or (Using Code/Nibs)
LoginViewController *viewController = [[LoginViewController alloc] init]; //initWithNibNamed in case you are using nibs.
//in case you want to start a new Navigation: UINavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
and push with:
[self.navigationController pushViewController:vc animated:true];
Also, if you are using Storyboards for the segues you can use this to do all the stuff. Remember to set the segue identifier.
[self performSegueWithIdentifier:#"pushLoginViewController" sender:self]; //Segue needs to exist and to be linked with the performing controller. Only use this if you need to trigger the segue with coder rather than an interface object.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"pushLiftDetail"]) {
[[segue.destinationViewController someMethod:]];
segue.destinationViewController.someProperty = x;
}
}
I think you'll need to add a UITabBar to the modal view and implement/duplicate the buttons and functionality that your main bar has. The essence of a modal window is it has total control until it is dismissed.
You might try putting your UITabBarController into a NavBarController, but I'm not certain that this will work.
UITabBarController -> NavBarController -> Modal View

UISearchBar and segues

Im using UITableViewController to manage data. When I click on the cell my program goes to another scene (let it be myDetailViewController). But i just added search bar (UISearchViewController) and search works great but when I click on the cell from the table with the filtered data nothing happens. But I need the program to go to myDetailViewController scene to view object details. How can i perform it?
Btw, for changing views (from a basic cells, not filtered) I'm using segues. And I'm also using this method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"nameofmysegue"]) {
//some code to give some data to new controller;
}
}
And when I'm using
tableView:didSelectRowAtIndexPath:
without segues nothing happens at all.
Make sure to connect the segue to the view controller itself and not the tableview row. Then in your didSelectRowAtIndexPath, you do a [self performSegueWithIdentifier#"mySegueID"];