I'm using storyboards with this. On the storyboard I have a "mainpage" uiviewcontroller that has a button which when tap will call another uiviewcontroller (choosewinner) to pop up. But this choosewinner vc has a uinavigation controller attached to it, reason why is so that it can go back and forth easily. Now I can call the uiview as a modal pop up but it doesn't show it's uinavigation.
This is the method called when the button is tapped. Thoughts?
- (void)updateWinner{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MatchWinnerViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"WinnerViewController"];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:NO completion:nil];
vc.view.superview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
vc.view.superview.bounds = CGRectMake(0, 0, 800, 600);
}
Even though you have the MatchWinnerViewController with UINavigationController attached to it in the storyboard, if you want to present it modally with the UINavigationController, you need to supply the navigation controller as the parameter to the method instantiateViewControllerWithIdentifier.
The reason is that for modal presentation, it takes the root view controller, and a navigation controller is also a view controller.
Related
I am not much familiar with segue. I have used it for very first time.
[self performSegueWithIdentifier:#"LoginSegue" sender:nil];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
id destinationViewController = segue.destinationViewController;
if ([destinationViewController isKindOfClass:[MFSideMenuContainerViewController class]])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:Main bundle:[NSBundle mainBundle]];
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:NavigationController];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)destinationViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:LeftSideMenuIdentifier];
[container setLeftMenuViewController:leftSideMenuViewController];
[container setCenterViewController:navigationController];
}
}
My problem is while I am trying to dismiss it on LOGOUT button which is in SideMenu using below method,
[self dismissViewControllerAnimated:YES completion:nil];
Nothing is happening. Don't know how to dismiss it.
Can anyone have solution for this?
Thanks in advance!
According the documentation dismissViewControllerAnimated
Dismisses the view controller that was presented modally by the
receiver.
So this works for modally presented controllers, for navigation stacks, use unwind segues instead.
[self dismissViewControllerAnimated:YES completion:nil];
Above statement dismisses a view controller when a view controller is present.
If you are using "PUSH" or "SHOW", then your view controller is pushing to navigation stack. Well in that case you have to POP that view controller from the navigation stack.
Try below code
[self.navigationController popViewController:yourViewController animated:YES];
My Tab bar should stay on screen when I click on button in Test2 View Controller.
I've set Test 3 View Controller's Bottom bar to inferred and tried Translucent tab bar. I've changed the segue from push to modal. I've tried this solution, but doesn't work for iOS7.
TestTabBarController.m this doesn't work either:
- (void)viewDidLoad
{
Test3ViewController * viewController1 = [[Test3ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *homeNavi=[[UINavigationController alloc]initWithRootViewController:viewController1];
self.viewControllers = [NSArray arrayWithObjects:homeNavi, nil];
}
This also doesn't work:
- (IBAction)buttonpress:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Test3ViewController * test3ViewController = (Test3ViewController *)[storyboard instantiateViewControllerWithIdentifier:#"test3View"];
[self.navigationController pushViewController:test3ViewController animated:YES];
}
First:
Your link Test2 to Test3 in storyboard is Modal,
change it to Push:
And u'll see your navigation bar.
Second:
Don't use the segue mechanism for your task.
Use the pushViewController on UINavigationController
A modal view will cover the tab bar. If you want to load a view after you hit the 'Button' do the following:
- (IBAction) loadNewView:(id) sender {
Test3ViewController * viewController1 = [[Test3ViewController alloc] initWithNibName:nil bundle:nil];
[[self navigationController] pushViewController:viewController1 animated:YES];
}
Then link that up with your button...
The answer is: Add a Navigation controller between Test Tab Bar Controller and Test2 View Controller.
current version of my project :
I have 5 different UIViewControllers in my app. I've set my
FirstViewController to be the Initial View Controller using the
Attributes Inspector. I move back and forth from one ViewController to
another by using buttons to which I assign modal segues, from one
ViewController to another, using the StoryBoard
What I want to change:
I want to keep the navigation buttons obviously, delete the modal segues and use
a UINavigationController instead. If I understand the concept
correctly, when using a UINavigationController I need to go into each
UIButton-IBAction and at the very end of the method I have to push the next
ViewController I want to move to, onto my NavigationController (do I also
have to pop the current one first?). However, I can't figure out how
to implement all that correctly.
What I've done so far:
I removed all modal segues from the storyboard and kept the navigation buttons along with their corresponding IBActions
I unchecked the box in the Attributes Inspector that was making my FirstViewController the initial View Controller of my app
I went into my AppDelegate.m and tried to create the Navigation Controller there and make my FirstViewController be the RootViewController
MyAppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *myFirstViewController = [[FirstViewController alloc] init];
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
[myNavigationController pushViewController:myFirstViewController animated:YES];
// Override point for customization after application launch.
return YES;
}
I then tried to test if the above was working by going into the IBAction of a
navigation button on my FirstViewController and implemented the
following in order to move to my SecondViewController when the
button is pressed :
FirstViewController.m
- (IBAction)goRightButton:(UIButton *)sender
{
// some code drawing the ButtonIsPressed UIImageView on the current View Controller
UIViewController *mySecondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:mySecondViewController animated:YES];
}
but nothing happens. What am I doing wrong ?
You are not linking your XIB file. Please add your navigation controller as
UIViewController *myFirstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
Use following code to move from one view to another
UIViewController *mySecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:mySecondViewController animated:YES];
If you are using a storyboard, you should just drag in the navigation controller there and hook it up to your app delegates. As long as it is the main storyboard, and you have identified a view controller to load first, you do not need to load any views in your app delegate.
In order to push a view programmatically that's in a storyboard, you need to do something like the following:
//bundle can be nil if in main bundle, which is default
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyCustomViewController *customVC = (MyCustomViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"customVC"];
//standard way
[self.navigationController pushViewController:customVC animated:YES];
//custom animation
[UIView transitionWithView:self.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
[self.navigationController pushViewController:customVC animated:NO];
} completion:nil];
You identify the view controller with the identifier you add in the storyboard editor. Below are some screenshots to help show what I mean.
I have a modal storyboard scene that I want to be accessible to all my other scenes. Creating a modal segue to it from every scene on my storyboard creates a big mess of strings going everywhere. Is there a way that I leave off the segues and call the scene programmatically instead?
Basically I want to do something like this:
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
[self presentModalViewController:myNewVC animated:YES];
except instead of creating and pushing a view controller class, I want to do a modal transition to an "isolated" (not connected with a segue) storyboard scene.
Yes you can. Do something like this to get access to the VC, then just Modal Push it:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
Note: the method presentModalViewController:animated is deprecated in iOS 6.
The new code should read:
NSString * storyboardName = #"MainStoryboard_iPhone";
NSString * viewControllerID = #"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller animated:YES completion:nil];
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code to bring that view forward.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
I have a case where I want to present a view controller from the main part of the app, one with settings & help & so on. To do this, I want it to be within a nav controller, sort of a little plug in module we can call from a UIBarButtonItem.
Now, this can be to/in the current storyboard, or to another, it doesn't matter.
I want to do it this way, because I loathe the potential of segue line spaghetti all over my storyboard.
Here's how to do it.
- (IBAction)displaySettings:(id)sender
{
LOG_SELECTOR() // google that for extra goodness
// FYI, this can be done using a different storyboard like so.
/*
NSString * storyboardName = #"MainStoryboard_iPhone"; // possibly use device idiom?
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
*/
// To push a new set of scenes with a new Navigation Controller, it is done like this:
UINavigationController *settingsNC = [self.storyboard instantiateViewControllerWithIdentifier:#"Settings Nav Controller"];
OBSettingsUIViewController *settingsVC = [self.storyboard instantiateViewControllerWithIdentifier:#"Settings root"];
[settingsNC pushViewController:settingsVC animated:NO];
[settingsNC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// Present the view controller;
[self presentViewController:settingsNC animated:YES completion:NULL];
}
In the presented view controllers (or in a subclass of the Navigation Controller), you can have a UIBarButtonItem to then dismiss the whole presented hierarchy of view controllers like so:
- (IBAction)dismissThisVC:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope this helps a bunch of people out. Cheers.
Just call viewcontroller using navigation controller
Write this code in viewcontroller and set viewcontroller in storyboard as set in the image.
ProfileVC *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"ProfileVC"];
[self.navigationController pushViewController:vc animated:YES];
Call to navigate to other class
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *navController = (UINavigationController *)window.rootViewController;
DumpFeed *dump = [storyboard instantiateViewControllerWithIdentifier:#"DumpFeed"];
dump.isPushed=YES;
dump.strUserId = appDelegate.strFriendid;
[navController pushViewController:dump animated:YES];
Heres a Swift version of this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewControllerWithIdentifier("myStoryId")
self.presentViewController(myVC, animated: true, completion: nil)
You should also change your storyboard id like this:
I think that with iOS7 it has become very easy implementing via the storyboard
I'm currently learning about the new features in iOS7 and found this simple solution, but it might have been relevant even in prior versions, I'm not sure.
First u need to connect the presenting VC with the target VC (thats the only connection needed), then within the storyboard's attributes inspector choose the style to be modal, in the identity inspector give your VC a storyboardID and make sure you checked the 'use storyboardID' checkbox,
If its not there yet add this method to your presentingVC:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
YourTargetVC * targetVC =
(YourTargetVC *)segue.destinationViewController;
if(nil != targetVC) {
//Do preparations here
}
}
Now, when you wish to show your targetVC from your presentingVC you can use:
[self performSegueWithIdentifier:(NSString *) sender:(id)];
where the identifier is your viewController's storyboardID, and the sender is the view who triggered the action, this method will invoke the storyboards scene, so the [prepareForSegue: sender:] method will be called allowing u making last modifications before the targetViewController will appear.
I have a view controller that is inside a popover, and I want to present a modal view controller from it. Here's my code:
EditDateViewController *dateViewController = [[EditDateViewController alloc] initWithNibName:#"EditDateViewController" bundle:[NSBundle mainBundle]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:dateViewController];
navController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:navController animated:YES];
[dateViewController release];
[navController release];
The result is this:
alt text http://cl.ly/5300e4f8f5d440d3f850/content
For some reason, the navigation bar background is transparent (or black?) even though I did not configure it that way. I tried manually setting the tintColor property of the navigation bar in the viewDidLoad method of the modal view controller, but it had no effect.
Try this
dateViewController.modalInPopover=YES;
self.navigationController.modalInPopover=YES;