I am newbie in iOS development and currently stuck on creating back button. I already placed popViewControllerAnimated method on tapping button, also I test it on debugging mode and back button method is calling but popViewControllerAnimated is not working.
Here is the code.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.height-100), 30, 80, 20)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(BackButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
//some piece of code...
}//end of viewDidLoad method
-(void)BackButtonClicked: (id)sender{
[self.navigationController popViewControllerAnimated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"testing" message:#"some message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"NO", nil];
[alert show];
}
On tapping back button this alert is showing but screen is not going back.
Any help regarding this issue will be highly appreciable.
I called this viewController by writing this code in my previous viewController
PlaceTranslatedDetailsViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"placeTranslatedDetail"];
[self presentViewController:secondViewController animated:YES completion:NULL];
However, this viewController and previous viewController were designed programmatically
If your viewcontroller is not an rootviewcontroller for a uinavigationcontroller than the method popViewControllerAnimated won't work. So you need to have a uinavigationcontroller behind in order to push/pop a viewcontroller.
So in your case you have to use [self dismissViewControllerAnimated:YES completion:nil]; instead of popViewController because you presented modally a view controller.
You are not presenting your view controller as part of a navigation stack, so using popViewControlleranimated: is not going to work.
You need to use
[self dismissViewControllerAnimated:YES completion:NULL];
instead.
use this code:
[self dismissViewControllerAnimated:YES completion:NULL]
A push (via pushViewController or storyboard push segue) will need a call to popViewController
A presented view (via presentVewController or a storyboard modal segue) needs a dismissViewController call.
Related
I am using AddressBookUI Framework for Adding contact, when I tried to pushing this view controller then cancel and done button not working properly, I don't want to present it
Here is my code
ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
[self.navigationController pushViewController:abnpvc animated:YES];
I am also tried add as subview rather then pushing it but when I am adding as subview then it was not added
As per comment i have tried like
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:abnpvc];
[self presentViewController:navigation animated:YES completion:nil];
Can anyone help me out why properly not working ?
You can implement that too considering also the other answers and the deprecations to ABNewPersonViewController in iOS 9.
As per your remarks:
cancel and done button not working properly
They are working if you have included the ABNewPersonViewControllerDelegate on interface like this:
#interface ViewController () <ABNewPersonViewControllerDelegate>
Pushing the viewController on navigation stack like this:
ABNewPersonViewController *controller = [[ABNewPersonViewController alloc] init];
controller.newPersonViewDelegate = self;
[self.navigationController pushViewController:controller animated:YES];
And by conforming to the protocol by implementing this method:
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person {
// Trick to go back to your view by popping it from the navigation stack when done or cancel button is pressed
[self.navigationController popViewControllerAnimated:YES];
}
The tricky line is to pop the newPersonController from the navigation stack when either Done or Cancel button are pressed.
Enjoy it
Why can't you just do it as the docs say?
It is recommended that you present a new-person view controller modally.
Use
ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
[self presentViewController:abnpvc animated:YES completion:nil];
That should work fine.
Edit
On second thought, did you set your delegate correctly and do the implementations get called? I suspect they are not implemented or the delegate is not set correctly.
Apple guideline(IMPORTANT) :: New-person view controllers must be used with a navigation controller in order to function properly. It is recommended that you present a new-person view controller modally.
Add Delegate
#interface ViewController () <ABNewPersonViewControllerDelegate>
Pushing the viewController
ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:abnpvc];
[self presentModalViewController:navController animated:YES];
And Now Add Delegate Method
#pragma mark ABNewPersonViewControllerDelegate methods
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
That will work fine.
I am using storyboard in my app where I have to navigate to another view controller without animation.I am successfully doing it using the custom segue.But I when I come back to the previous view controller then by using navigation bar default back button then it is animating while coming backward.So going forward it is not animating and going backward it is animating.I want to stop the default animation of the back button.
Custom Seague
// PushNoAnimationSegue.h
#interface PushNoAnimationSegue : UIStoryboardSegue
#end
// PushNoAnimationSegue.m
#implementation PushNoAnimationSegue
-(void) perform{
[[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO];
}
#end
Using this code I am successfully going to the next view controller without animation since -(void) perform is called automatically in the forward direction.When I come back it is not called.So please suggest me if there is anything wrong with the implementation or I should go with some other method.
hope this will help
1.custom button on navigation bar
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width,height)];
[btn setImage:[UIImage imageNamed:#"some_image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem =backButtonItem;
and then
void popViewController()
{
[self.navigationController popViewControllerAnimated:NO];
}
If you use NavigationController then use this
[self.navigationController popViewControllerAnimated:NO];
Or
[self dismissViewControllerAnimated:NO completion:nil];
I am developing iPhone app, where i got stuck at one point.
The error i am facing is, i have one presentViewController on which i have one button, on click of that button i am dismissing my presentViewController and after dismissing i want to push My view controller but it doesn't pushes.
Code on PresentViewController:
- (IBAction)ButtonClick:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
[Obj MethodCall];
}];
}
after dismissing PreviousViewController:
-(void) MethodCall
{
NSLog(#" -- MethodCall Success --");
[self.navigationController pushViewController:[[NewViewController alloc] init] animated:YES];
}
My log shows -- MethodCall Success -- but it does not pushes my view.
Where i am doing mistake ?
Please help and thanks for reading.
The problem with you is I think you are not having UINavigationController as a rootViewController of the viewController from which you have to navigate. So please try to add a UINavigationController as a parent view or rootView of the vieController from which you want to push another viewController.
- (IBAction)ButtonClick:(id)sender {
NewViewController *newView = [[NewViewController alloc] init]
[self presentViewController:newView animated:YES completion:nil];
}
I am currently working on a game for the iOS platform. The game's initial view controller is a login screen, and once the player logs in, he gets to a loading screen, and then a navigation view controller containing a table view with a list of current games. The storyboard's relevant view relations look like this:
[login screen] --modalsegue-> [loading screen] --modalsegue-> [navigation controller] --relationship-> [tableview (with logout button)]
I want to make a logout button at the bottom bar of the navigation view's [table view], which takes the player back to the [login screen]. How can I do this?
Thanks in advance!
You can dismiss, self.presentingViewController.presentingViewController or you can use dismissToRootViewController.
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *btnLogout = [[UIBarButtonItem alloc]initWithTitle:#"Logout" style:UIBarButtonItemStyleBordered target:self action:#selector(btnOnClick:)];
self.navigationItem.rightBarButtonItem = btnLogout;
}
-(void)btnOnClick:(id)sender
{
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];
}
I hope...This may help you...
I read a lot of documentation about this before i decided to ask.
So I have navigationController in my app. When user enters the first time in my app I do this
RegViewController *opr = [[RegViewController alloc] init];
self.regController = opr;
[self.navigationController presentModalViewController:self.regController animated:NO];
[opr release];
And it works fine. But when I click OK button in RegViewController I call this method
-(IBAction)btn_regPressed:(id)sender
{
NSLog(#"start to dissmis modal");
[self.navigationController dismissModalViewControllerAnimated:YES];
//[self.navigationController.parentViewController dismissModalViewControllerAnimated:YES];
}
But this code doesn't want to dismissModalViewController
Can somebody help me with thios issue please. Thanks
try to change your call to (remove the navigationController):
[self presentModalViewController:self.regController animated:NO];
and in your button (remove the navigationController):
[self dismissModalViewControllerAnimated:YES];
You are telling to your navigationController dismiss your modal, but your viewcontroller should do it.
Navigation controllers are used to navigation (go and back) purposes. And probably, your navigationController is nil.
In your RegViewController, try to dismiss self like so:
-(IBAction)btn_regPressed:(id)sender
{
NSLog(#"start to dissmis modal");
[self dismissModalViewControllerAnimated:YES];
}