How to dismiss segue ios8 - objective-c

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];

Related

Presenting the UIDocumentPickerViewController and dismissing it keeps the presented view controller in freeze

How can I present the UIDocumentPickerViewController and dismissing it keeps the presented view controller in freeze
Problem is that you open UIDocumentPickerViewController in PDFBrowserViewController so when you dismiss UIDocumentPickerViewController but PDFBrowserViewController was not dismissed.
solution 1: open UIDocumentPickerViewController directly in top viewcontroller without use of PDFBrowserViewController.
solution 2: after dismiss UIDocumentPickerViewController dismiss the parent controller in Delegate methods.
UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
UIViewController *vc = [window rootViewController];
[vc dismissViewControllerAnimated:true completion:nil];

viewcontroller with a uinvaigationcontroller when pop up does not show the navbar

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.

Add NavigationController to current view

I'm using a UIScrollView to display some images, and i need to present or push another view when the user selects one part of the image.This ScrollView is created only programmatically.
How i can programmatically add a navigationController to it and present another view?
I tried this way but just give me the following error :
Pushing a navigation controller is not supported'
StatesViewController * controller = [[StatesViewController alloc]initWithNibName:#"StatesViewController" bundle:nil];
UINavigationController * Navcontroller = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController: Navcontroller animated: YES];
this might help you.
NextViewController *nextViewController=[[NextViewController alloc]initWithNibName:#"NextViewController" bundle:nil];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:nextViewController];
[self.navigationController presentModalViewController:navBar animated:YES];

I have no Navigation Bar in a view called with an IBAction?

My main menu (a ViewController) is embedded in a NavigationController that I added in the storyboard in Xcode4.
I have a button in my menu, displaying a new view. To display it I use :
- (IBAction) display : (id) sender
{
if(!anotherView) anotherView = [[AnotherView alloc] initWithNibName:#"AnotherView" bundle:nil];
[self presentModalViewController:anotherView animated:NO];
}
My other view is correctly displayed with all its objects and elements. Excluding my NavigationController's bar, that doesn't appear. Why ?
Thanks for your advices
You are presenting the view modally what you probably meant was
[self.navigationController pushViewController:anotherView animated:YES]
of course what you really want to do is not mix and match storyboard and non storyboard flows unnecessarily like this and have the storyboard do this for you
you are presenting your viewController modally. If you want to use the navigation controller you have to push your view onto the navigation stack.
replace
[self presentModalViewController:anotherView animated:NO];
with
[self.navigationController pushViewController:anotherViewController animated:YES];
You can still present your view modally without losing the navigation bar. Try this code:
AnotherView *tempAnotherView = [[AnotherView alloc] initWithNibName:#"AnotherView" bundle:nil];
[self setAnotherView:tempAnotherView];
[tempAnotherView release];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:self.anotherView] autorelease];
[self.navigationController presentModalViewController:navController animated:YES];
Hope it helps! :)

Call storyboard scene programmatically (without needing segue)?

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.