How to go to Previous View? - iPhone Devlopment - iphone

I am Using a Navigation based application.
However i don't want to push a view.
I have changed present ModalViewController.
Now, I am confused - how to load previouse view.
My Application's Table View - default in navigation base application
On navigation controller I have added (add employee) button.
On Add(employee) click i have written following code in appDelegate.m
-(IBAction)AddClicked:(id)sender
{
if(self.addData==nil) // addData is a object of my next view
{
addData=[[AddEmpScrn alloc] initWithNibName:#"AddEmpScrn" bundle:nil] autorelease];
}
[self.navigationController presentModalViewController:self.addData animated:YES];
}
This code works successfully & next Add Employee screen load perfactly.
But Now I am confused.
How to get back to previous view?
that is if a user click on "Save" or "Cancel"
it should display again the previous view...
main screen
I dont know how to solve it...
anybody please help me...
i will be thank full.

Have u tried using the dismissModalViewController method? should do what you are asking f or

Have you read "Using Modal View Controllers"?
If you presentModalViewController then the old view is still there "behind" it. You just need to call dismissModalViewControllerAnimated: on the original navigation controller.
If you are directly manipulating the view hierarchy by swapping out subviews, you will need to hold onto the old view in another member variable and make sure to retain it.

Related

When using popToRootViewController I lose my navigation bar

I have an iOS app where the main screen is a UICollectionViewController. When selecting an Item from the collection view the view is pushed to a detail view of the item. In the detail view I built a drawer/slider that moves out from the side. In order to get the view to look the way I wanted I hid the default navigation bar and inserted one via storyboards.
I ran into an issue that when hiding the default navigation bar you lose the back button functionality that comes with using a navigation controller. I worked around this by adding a button where the back button would have been (the image above is shown without the button). Now I use the line of code below to move back to the collection view.
[self.navigationController popToRootViewControllerAnimated:YES];
It works the way I want it except that I lose my Navigation Bar when I return to the collection view. Does anyone have any thoughts on how to fix this? Thanks in advance!
In viewWillAppear of your rootViewControler
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO];
}
I know this thread it's a little dated (more than dated more like an archive). But I ran into the same issue in swift 5, Xcode 13. Below is the swift code that I used in my viewWillAppear in case it helps someone.
self.navigationController?.setNavigationBarHidden(false , animated: true)

iPhone Structure ViewController

I am designing an iPhone application with a home page. This page has multiple buttons (6) that go to different things.
2 buttons are a simple view that just have some information and go back to the home screen. The next button opens up an email and I believe that will just be one view, so not a whole lot different than the other two.
Here is where it gets complicated. One button will take a picture, and another will select one from the library. Once that is done it will edit it and create an object that I will create. That object will be stored in an array, which will be opened by the last button one the home page and a UITableViewController will control that.
My first question is should I use a navigation based view controller or just a view controller that I can create myself? Or should I use something that I don't even know about?
Please Help!!! And if you help a sincere thank you!
EDIT:
Well i tried it my own way first and the only issue i'm having is this code
- (void) displayView:(int)intNewView {
NSLog(#"%i", intNewView);
[home.view removeFromSuperview];
Instructions *i = [[Instructions alloc]init];
instructions = i;
[self.view insertSubview:instructions.view atIndex:0];
}
It is in my SwitchClass, which controls the Main Window's view. I know it is working there because when it first runs the switch class directs it to the home screen. I know the method is being called because the console is displaying the NSLog thing, but it just won't switch.
Aside from the fact that you have 6 buttons, I would try and use a UITabBarController for what you are trying to do; it would seem more natural to me (but you should find a way to reduce you 6 button to 5, otherwise they will not be displayed all at once).
Otherwise, a UINavigationController seems fine to me. For each button you push a new controller to deal with that button functionality, then you pop back. It should work easily.
EDIT:
have you tried with?
[self.view addSubview:instructions.view];
Your first question Yes you should use navigation based controller ... so when you press any button will open the other view controller with animation.. also Navigation Based Controller keep track of the parent controller if you have any created objects will be retained in the parent view controller that is the root of the Navigation.
here is the steps that you should use.
1-Create Navigation controller in the main application delegate and make it's root is the view controller.
2-when you push the view controller that have 6 buttons .
3- you can check this link for get photo album also if you have changed the source type to camera then you can get the image...
Photo Libaray
4- once you get the image you can add it to NSMutableArray that exist on the NavigationViewController root in your case will be the view which have the 6 buttons.
5-sice every time you want to view the array which contain the photos you will initialize the data source of the uitableviewcontroller with the array that you save photos on.
Thanks
I think the problem is coming from one of two places:
As I understand it, these are all different View Controllers, correct? And they have their own xib files? If that is true, then calling:
Instructions *i = [[Instructions alloc]init];
is insufficient. You need to use
Instructions *i = [[Instructions alloc] initWithNibNamed:#"Instructions"];
in order to include that view that you have already constructed in the interface builder.
The other thing I see potentially going wrong is that you are inserting all the views at the same index. Think of the index as a layer in photoshop. If you want the new view to be visible overtop of the last one, then it needs to be a higher index. This is handled automatically if you use addSubview: instead of insertSubview: atIndex:

How do I remove a view controller from my view hierarchy?

My app has a welcome screen that can only be shown in portrait mode. After the user has tapped through the welcome screen I'd like to show another screen that can be used in both portrait mode and landscape.
I have set up a view controller that implements shouldAutorotateToInterfaceOrientation: returning YES only for UIInterfaceOrientationPortrait, and I add the view to the window with [window addSubView:view]. I tag this view with the tag 1.
When the user taps through the welcome view and the app moves on to the new view I do:
[[window viewWithTag:1] removeFromSuperView];
[window addSubView:myViewController.view];
Where myViewController is an instance of the 2nd view's view controller (that handles the shouldAutorotateToInterfceOrientation method properly).
Now when I rotate, it still calls shouldAutorotateToInterfceOrientation on the original view's view controller, and does not call it on the new view's view controller.
This note from Apple says that only one view controller will get rotation notifications; however, I have removed the other view controller.
What am I doing wrong?
actually that note doesn't say that "only one view controller will get the notifications" but instead it says that "Only the first view controller added to UIWindow will rotate.".
So this might be the problem.
To resolve it, i would say to always have a view added to your window (call it permanent), and add your welcome screen and the next views to this permanent view.
Hope this helps.
as the note you link to state:
Only the first view controller added
to UIWindow will rotate.
So put a flag that makes sure that shouldAutorotateToInterfceOrientation returns NO until the user have dismissed the screen - and then returns YES afterwards. This is a simple and working solution - however, from a code readability point it might be confusing that a "dismissed" view actually controll the rotation.
Personally; my experience is that it's not really worthwhile having some views rotating and some don't - and users tend to don't like it.
happy coding
I wrote up a quick test that shows what you are trying to do should work. Here are the basics I did:
Create two view controllers. App starts with the first view controller being set in the AppDelegate into an instance variable viewController through NIB files. It is then added to the window as you have written.
I then setup an action that when called (could be a Timer, button on first view controller, etc.) that performed the following:
Remove view using [self.viewController removeFromSuperview]. This is different than the way you have done with the tag.
Created second view controller and assigned it to self.viewController.
Added to window like you have specified.
Not sure what is incorrect with your code. I would guess that perhaps the first view wasn't really being removed.

how to get back to one view to another view in view based application

I did a simple view based application.
what i did is creating view additional to existing view.
means there is one view named main in .xib file additional to that i am creating another view
IBOutlet UIView *view_additional;
In the main view by button click i call the view_additional view.
For the view_additional view i place navigation bar with one bar button named back.
In the back button click event i need to get back to main view.
for that in the button click action i write the fallowing code.
[view_additional removeFromSuperview];
But it is not open main view,shows a white screen.
what the wrong.
how can i get back from additional view to main view.
Thank u in advance.
for the code you used to display view_additional, instead of using self.view = view_additional
try using, on button click
[self addSubview: view_additional];
And once that's done, your code should work just fine the way you want.
When dealing with views, if you change a view, then remove it, as you did in your code, then all that will be left is the main app window. There will be no views on that window. From the look of it, you want to load up a new view, and then remove it once you're done with it. You can do this by adding the new view as a subview instead of totally changing the parent view. Another thing you can do is set an IBOutlet for the original view as well, this way you'll have an outlet for both views, and then instead of removing your additional_view from the superview, you can just switch it back. So the code for the Back Button would just be
self.view = original_view;
on back button click. I hope this isn't too confusing for you. Let me know if you have any questions.
Both methods would work for what you want to do.
I resolve my problem as
instead of [self.view = view_additional];
i use
[self addSubview: view_additional];
To get back to original view in the button click
i use
[view_additional removeFromSuperview];
MahesBabu, that is exactly What I suggested that you do. Only I offered the background info on why you should do it that way.
# MaheshBabu:
This is the common code u can use to go on next view or go on back, Just change button name which u want to press and the view name on which u want to go. For example on back button press u want to go on library view then write this code -
-(IBAction)backButtonPressed:(id) sender
{
libraryView *libraryview=[[libraryView alloc] initWithNibName:nil bundle:nil];
libraryview.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:libraryview animated:YES];
[libraryview release];
}
Let me know whether ur problem is solved or not.

I am not able to refresh the old view on button click

I have viewBased Application, i have 3 view controller in that.
When I want to go back to first view from second view, I want that view to be reloaded from start.
Code I use to dismiss the present view is:
//Back Button Code:
[self dismissModalViewControllerAnimated:YES];
//Code I use to go to new view is:
[self presentModalViewController:secondView animated:YES];
I am not getting what you trying to do...
put your logic inside viewWillAppear method... which gets called everytime view Loads..
When I want to go back to first view from second view, I want that view to be reloaded from start.
Wich one?
Well I think it should work since dismissing a modal dialog unloads it. What happens exactly that you don't want? You must have retained something you don't want to.
It's not clear if you want your first view to restart, or your second view to restart.
Answer for both cases:
If you want view 1 to restart, move the code you need re-executed into viewWillAppear()
If you want view 2 to restart, it's been released from memory so going back to it will call viewDidLoad() and restart it anyway.
Hope that helps....