ios how simulate back button in a ViewController - ios

i have more viewcontroller that start from rootviewcontroller.
So for example i start with controller A so pass to B and go to C the third.
Normally to come back we need to push back on left top.
Instead i want to know if is possible instead use back button by code come previus view controller that in this case is B.
to go on the next i use this
ViewController *sc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[[self navigationController] pushViewController:sc animated:YES];

Simples
[[self navigationController] popViewControllerAnimated:YES];

See UINavigationController popViewControllerAnimated:(BOOL)animated

Related

Cannot click back button when open view from remote notirication

My iOS application using Objective C. It received a remote notification, when a user click it, it will open a particular view.
This view has a back button in the navigation tab. In normal scenarios can back to root view. However, when I open this view from a notification, cannot back to any view nor leaving the application.
Here is my spruce code:
1. from AppDelegate.m to open a particular view:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewAnnouncement =[storyboard instantiateViewControllerWithIdentifier:#"Announcement"];
self.window.rootViewController = viewAnnouncement;
[self.window makeKeyAndVisible];
Action function to back to previous view (in viewClass.m):
- (IBAction)onBtnBackClicked:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
I believe the problem is, when I open this view from a notification, no parent view to let it back to. Can anyone help me, when user open this view from a notification, whether it goes to a root view or back to the previous opening app
EDIT : The previous answer didn't take in count the fact that you were in AppDelegate. Maybe this answer is better.
Turn your [self dismissViewControllerAnimated:YES completion:nil]
into :
UINavigationController *navigationController = [[UINavigationController alloc] init];
UIViewController *yourPreviousVC = [[UIViewController alloc] init];
[navigationController pushViewController:yourPreviousVC animated:YES];
If I'm wrong or if I missunderstood the question, do not hesitate to correct my answer.
Try this
Announcement *annouce = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Announcement"];
HomeVC *home = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"HomeVC"];
[((UINavigationController *)self.window.rootViewController) setViewControllers:#[home,annouce] animated:YES];
And If app already open and you want to go previous page
Announcement *annouce = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Announcement"];
UIViewController *visibleView = ((UINavigationController *)window.rootViewController).visibleViewController;
[((UINavigationController *)self.window.rootViewController) setViewControllers:#[visibleView,annouce] animated:YES];
I found the solution. My app has multiple views which I can go from a navigation list. When I want to back to main view, I just dismiss the current view.
Cuurently, a view is opened when I click a notification and I can't simply dismiss this view, Coz it considered as root view.
What I did to solve it, I connect my view (Announcement) by segue to main view, and set an identifier (for example: "leaveAnnouncementPage". Then, in your class just call this function in back button's fnction to back to the main view:
[self performSegueWithIdentifier:#"leaveAnnouncementPage" sender:self];
Here is also some useful links:
First
Second
Thank you guys for your help, and all the best

Attempt to present a viewcontroller whose view is not in the window hierarchy

I have a tab bar application. My requirement is I select the 'scan' tab to scan the qr code and navigate/jump immediatley to another 'list' tab. Both 'scan' and 'list' tab are there in the viewControllers array in didFinishLaunchingWithOptions After referring this link, i don't think i need to set the delegate as both the tabs are already present in the hierarchy.
I get this warning in the following line
if(x)
{
listViewCntrl = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
listViewCntrl.getFlag = YES;
[self presentViewController:listViewCntrl animated:YES completion:Nil]; // I get the warning here
}
If I comment out the above code and add
[self.tabBarController setSelectedIndex:1];
then I would not be able to get the subView of the listViewController (set flag to show the subview) which i need to display inside the list tab after scanning.
App crashes if I add
[self.tabBarController setSelectedViewController:listViewCntrl];
So how do I display the listView's subview after scanning?
You can try this if you use storyboard:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController * destViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:destViewController animated:YES];
You have to set an identifier for your controller in your storyboard.
ListViewController *listController = (ListViewController*)[self.tabController viewControllers][1];
listController.getFlag = 1;
[self.tabBarController setSelectedIndex:1];
The problem is that you're creating an entirely new ListViewController. You say you already have one in the tab controller - you don't need a new one.
You can't use your 3rd option because, again, the two ListViewController's are different objects (they may be of the same class, but they point to a different address).

Push a new ViewController and Pop the last one from NavigationStack

Hey Guys i want to push a new controller onto the navigation stack and then remove the controller where i pushed from.
Here is my Code :
WishDetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"WishDetailView"];
detailView.transferWishID = [NSNumber numberWithFloat:[[response objectForKey:#"id"]floatValue]];
[self.navigationController pushViewController:detailView animated:YES];
[self.navigationController popViewControllerAnimated:NO];
Everthing works fine, but i got this message here inside the console :
2013-02-05 10:32:42.029 BWMApp[1444:1a603] nested pop animation can result in corrupted navigation bar
2013-02-05 10:32:42.392 BWMApp[1444:1a603] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
So what i am doing wrong and how can i prevent my app from throwing this error message ?
You can use setViewController. This example removes all and insert others, but give you the basic idea :)
NSMutableArray *viewCons = [[[self navigationController]viewControllers] mutableCopy];
[viewCons removeAllObjects];
[viewCons addObject:portraitTemp];
[viewCons addObject:self];
[[self navigationController] setViewControllers:viewCons];
There is no need to pop the "old" viewcontroller. The navigationController create a backbutton automatically. if you pop the viewcontoller from the stack there is no viewcontroller to "jump" back. This is the cause of message inside the console. The navigationController can't work correctly.
WishDetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"WishDetailView"];
detailView.transferWishID = [NSNumber numberWithFloat:[[response objectForKey:#"id"]floatValue]];
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:detailView animated:YES];

How do I set UIButton to go to the previous screen?

I have a help screen that can be accessed from any other screen. The only button there is a "back" arrow. Right now it always goes back to the main screen. However, I need it to go back to which ever screen that was before it. I've hunted through stackoverflow and google but no luck.
EDIT
snippet of the code I'm using now:
appViewController *homeView = [[appViewController alloc] initWithNibName:nil bundle:nil];
homeView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:homeView animated:YES];
From the modal view controller that is being presented right now, make it dismiss itself when clicking the home button:
- (void)buttonClicked
{
[self dismissModalViewControllerAnimated:YES];
}
On button click You can use.
[self.navigationController popViewControllerAnimated:YES];
Maybe this link will help you: How to navigate previous screen from camera view in iphone?
Its about navigation and how to remove the last screen.
Or use this in the click event of button.Create an instance of the view controller to be opened
EnterNameController *newEnterNameController = [[EnterNameController alloc] initWithNibName:#"EnterName" bundle:[NSBundle mainBundle]];
[[self navigationController] pushViewController:newEnterNameController animated:YES];

UINavigationcontroller pushViewController not working to get next Viewcontroller

I red nearly all the help which is provided, and i'm still confused. I have simple project in Xcode4.2 with sotoryboard. There i have Navigationcontroller and two Viewcontroler (one, two). If i'm trying to get from one to two with pushViewController its break down. Where is my problem. If i have the Navigationcontroler in my storyboard i shouldn't have problem to access it and use it, is it right? I don't won modal window application i use something like story board where the user can move back and front without lose the controls data. How should i do it right.
thanks for any help I'm new to apple developing :)
The only code :
- (IBAction)Next:(id)sender{
two* myNext = [[two alloc] initWithNibName:#"two" bundle:nil];
NSLog(#"%#", [self navigationController]);
NSLog(#"%d", [[[self navigationController] viewControllers] count]);
[[self navigationController] pushViewController:myNext animated:YES];
// [[self navigationController] pushViewController:myNext animated:YES];
// [myNext release];
}
The solution is to change the initialize UIViewController because of using storyboard you can't use initWithNibName. You have to use instantiateViewControllerWithIdentifier which use identifier property of UIViewController. The identifier property can by set in the sotryboard.
ViewController* myNext = [self.storyboard instantiateViewControllerWithIdentifier:#"Next"];

Resources