Ad the title says, I'm wondering how I move back to a already allocated view. In this case I have three views, Mainmenu, Main_game_view and game_over_view. I want to move back from game_over_view to mainmenu without reallocating it. Also note that I'm using xib files
How do I do that?
If you use storyboard, you can create a segue by control-dragging.
If you move from viewController1 to viewController2 by [viewController1 presentViewController:viewController2 animated:YES completion:nil];, you can use [viewController2 dismissViewControllerAnimated:YES completion:nil]; to go from viewController2 back to viewController1.
If you use a UINavigationController and push viewController2 by [someNavigationController pushViewController:viewController2 animated:YES];, then you can do [someNavigationController popViewControllerAnimated:YES]; to pop the view controller and go back to the first view.
If it's multiple views presented on the same view controller, then you can remove the views from their super view using [someView removeFromSuperview] and leave the first view only.
Hope it helps :)
========
Edit:
You can create a reference of your Mainmenu in your Game_over_view view controller:
In Main_game_scene view controller's .h file, create #property (strong, nonatomic) UIViewController *mainMenuViewController;. After Mainmenu creates Main_game_scene view controller, do main_game_scene.mainMenuViewController = self;.
In Game_over_view view controller's .h file, also create #property (strong, nonatomic) UIViewController *mainMenuViewController;. After Main_game_scene creates Game_over_view, do game_over_view.mainMenuViewController = self.mainMenuViewController;.
Now you have a reference of Mainmenu in Game_over_view, just call [self.mainMenuViewController dismissViewControllerAnimated:YES completion:nil];.
Related
I have a main view controller that is able to process gestures in an iPad app.
I launch a second view controller via:
wVC = [self.storyboard instantiateViewControllerWithIdentifier:#"vc_webView"];
[self presentViewController:wVC animated:YES completion:nil];
If I now gesture while that VC is showing, the gestures are not processed. How can I "pass" the gestures to the first storyboard for subsequent processing so that I don't need to rewrite the whole gesture functionality in the new VC?
OK so the answer is to use delegates.
For completion's sake, here is the way to do it:
Set up the first VC to be a delegate of the second, then in the second VC call the delegate function.
In original controller right before presenting the wVC:
wVC.delegate = self;
In the wVC.h file:
#protocol senddataProtocol <NSObject>
-(void)ProcessPasswordGesture:(NSInteger)iGest;
#end
#property(nonatomic,assign)id delegate;
In the wVC.m file:
#synthesize delegate;
[delegate ProcessPasswordGesture:<data>];
Hope this helps someone else!!
I make a Tabbed Application using storyboard template, two view controllers are embedded.
This is what I want to do: in the first viewController, let TabBar to select the second viewController programmatically.
The first viewController is a tableViewController, shows a list of items, and each item will push to a detailViewController. In the detailViewController, I edit some information and save the item. Then I want app to show the second ViewController, which is a tableViewController shows saved item.
Usually, we can use [TabBarController setSelectedIndex:1]; to select the second viewController.
However, since this is a storyboard template application, so many code are hidden behind. So I cannot get the TabBar instance in the first viewController, and use setSelectedIndex method.
This is what confuses me...
And now, I have found the solution for this problem. My answer is below.
I have figured out how to solve this problem.
First I add new a class: MyTabBarController.
Then, in storyboard, select the Tab Bar Controller, in identity inspector panel, set the custom class to this new class.
For the first viewController class, add a property
#property (nonatomic, weak) UITabBarController *tabBarController;
Then add - (void)viewDidAppear:(BOOL)animated in MyTabBarController class:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UINavigationController *navigationController = [self.viewControllers objectAtIndex:0];
FirstViewController *firstViewController = (FirstViewController *)navigationController.topViewController;
firstViewController.tabBarController = self;
In this way, I pass the tabBarController instance to the firstViewController, so, in the firstViewController, I can call [tabBarController setSelectedIndex:1];
Storyboard gives me a visual interface, however, it hides so many things behind.
My app have a main screen (mainView). On mainView, I have a button. When user taps it, I use
[self presentViewController:libraryView animated:YES completion:nil]
to present another view (libraryView). Then, on libraryView, I use
[self.view addSubview:tabBarController.view]
to add a UITabBarController which has 2 View Controllers: featuredBooks and recentBooks
Everything works fine. But when I add a button to featureBooks to dissmiss the libraryView and return to mainView, the following methods don't work
[self dismissViewControllerAnimated: YES completion:nil]
[self.parentViewController dismissViewControllerAnimated: YES completion:nil]
[self.presentingViewController dismissViewControllerAnimated: YES completion:nil]
I know the reason: self refers to featureBooks's view, not libraryView.
So, how do I refer to libraryView, dismiss it and return to mainView from a view controller (featureBooks or recentBooks) inside Tab Bar Controller?
Thank you very much.
Make mainView delegate object of libraryView... Then when you call the delegate method from libraryView, mainView will call in its code the method dismissViewcController.
So:
1)create into libraryView controller .h code:
#protocol LibraryViewDelegate
- (void) LibraryViewDelegate_DismissButtonClicked;
#end
2)then create a property into mainView .h file:
#property(nonatomic, assign) NSObject<LibraryViewDelegate> *delegate;
and the following into the .m one
#synthesize delegate;
and assign mainView to this property after libraryView object creation and before you will present it
3)write the following code into the mainview .m file:
-(void)LibraryViewDelegate_DismissButtonClicked{
//put here the code for dismissing mainView created modalViewController (libraryView)
}
4)then write the code that call:
[self.delegate LibraryViewDelegate_DismissButtonClicked];
into libraryView when you press the dismiss button
I have a view with 3 buttons and a tabbar controller that contains 3 views. I am using the storyboard. I want go from my view to a specific view from the tabbar controller. When I create a segway to the destination view, the tabbar is not included.
The only way I find out is to create a segway to the tabbar controller itself, but then by default the first view is been shown.
Thanks in advance!
Yess!! I got the solution. Do the following:
In you're .h file:
#property (strong, nonatomic) UITabBarController *tabController;
In you're .m file:
#synthesize tabController;
tabController = [self.storyboard instantiateViewControllerWithIdentifier:#"tabbar"];
The selected index is the tab you want to go
tabController.selectedIndex = 1;
[[self navigationController] pushViewController:tabController animated:YES];
I currently have a view set up as the following:
#interface BlogViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *mainTableView;
}
#property (nonatomic, retain) UITableView *mainTableView;
As you can see, it has a UITableView inside of it that I load all of my data to. However, when I call the following function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SingleBlogViewController *viewController = [[SingleBlogViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
//[self presentModalViewController:viewController animated:YES];
[viewController release];
}
nothing happens. For some reason my UITableView inside of my UIViewController isn't pushing the view. Is this because it isn't a UITableViewController? I tried changing it to that, and it still didn't work.
Am I missing something here?
I found the first parts of this blog post useful for showing how to create and use a UINavigationController programmatically without Interface Builder.
Some of the things I wish the docs and tutorials would have stressed to me:
When you create the UINavigationController you get a UIView and UINavigationBar for free (i.e. you don't need to separately add them and figure out how to hook them together).
You add the myUINavigationController.view property as the "main" view and then push/pop UIViewControllers onto the UINavigationController and they will automatically show up as visible in the myUINavigationController.view UIView.
When you push a UIViewController onto a UINavigationController, the UIViewController.navigationController is filled in. If you haven't pushed the view onto the navigation controller, I'm guessing that property is empty.
The time/place to programmatically add buttons to the UINavigationBar is not when and where you construct the UINavigationController, but rather by the individual UIViewControllers when they are loaded as you push onto the UINavigationController.
I only had to add a "Done" button to the UINavigationController's UINavigationBar in the viewDidLoad method of the first UIViewController pushed onto the UINavigationController. Any UIViewController I pushed on top of that first view automatically had a "back" button in the to navigate to the covered up UIViewController.
If you set the title property of your UIViewController before you put it on the UINavigationController's stack. The UINavigationBars title will be the title of your view. As well any "back" buttons will automatically name the view you are returning to instead of generically saying "back".
Please visit "How to add UINavigationController Programmatically"
Please visit above link & get all information regarding UINavigationController.
UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease.