Save and Restore UIViewcontroller when back button is pressed - iphone

I have two UIViewControllers. In first UIViewController I have a UITableView as a subview. When any one of the UITableViewCell is selected, a UIView(as a subview) will be displayed, which have 9 buttons. When any one of the button is pressed, Second UIViewController will be presented. When user press custom back button on second UIViewController, First UIViewController have to be presented with all previous selected UITableViewCell and corresponding subviews open. How to achieve this. Is there any process to save UIViewController's state at a desired time.
Any suggestion will be greatly appreciated.

If your are using a Navigation controller, navigating from one controller to another, your parent view controller will not be deallocated. Hence you can maintain state of that first view controller by means of properties or something else. When viewWillAppear is called for that controller, you can make use of that and update the controller accordingly.

Related

Manuel segue 'Show' does not call ViedDidload when returning to parent viewController?

I have a viewController containing a UITableViewController. This UITableViewController is populated by an array. When User presses populated cells they segue with a segue set to 'Manuel Segue : Show' this creates a back button UINavigation bar item. Inside this viewController the user is able to add Items to the array populating the parent UITableViewController. The problem is that when i segue back using the UINavigationbarItem it does not call viewDidload on the UITableViewController, there by not updating the UIViewTableCells. I have to close application to make it call viewDidload... How do i make it call ViewDidload when returning from the Manuel segue show? All help appreciated.
The pushing view controller is not unloaded when another view controller is shown above. As viewDidLoad: is only called once in the view lifecycle, this would then not be called when the segue is unwound.
Updating your tableview in viewWillAppear: or viewDidAppear: would cause this to be called whenever this view is displayed.
viewWillAppear would perhaps be better if you don't want to show the user the table reloading when not needing to asynchronously load data.
Read more on iOS view controller lifecycle here:
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html#//apple_ref/doc/uid/TP40015214-CH6-SW3

Why does my view in a Navigation Controller clear after hitting the back button?

I have two UITableViewControllers in a navigation controller with static cells that I am using for my form. I have a show segue from a cell on the first view to the second view, but when I hit the back button on the second view, all of the data in the text fields and pickers in the second view is erased. Why is that and how can I fix it?
The second view controller is created every time you perform the segue from the first to the second view controller. Therefore the initial state of the second view controller is re-created every time new. See this answer to understand the process better (It's the answer to question 3):
iOS Segue - When are the viewControllers Instantiated
You have many options to resolve this. E.g:
store the data in the second view controller on disk (e.g. with NSUserDefaults) and re-create if every time the view controller is created
store an instance of the second view controller in the first view controller and do not segue to it but call self.navigationController?.pushViewController to open the second view controller
etc...

interact the navigation controller bar button with embed container view

I've created a UIViewController, and insert a container view in it, which embed a UITableViewController. As the image described above.
When user click the Table View Cell, I'd like to add a UIBarButton on the Navigation bar.
But, how can I manage this? I can rise the DatePicker when click on Table View Cell, but when I call self.presentingViewController in table view controller implementation file, it returns (null), same as when I call self.parentViewController
You're probably trying to access the parent controller too early. If you log self.parentViewController in viewDidLoad, it will return null, but it should return the correct controller from viewDidAppear, and certainly from the didSelectRowAtIndexPath method. Using parentViewController in this context is correct, not presentingViewController.
I'd suggest to implement the UITableViewController Delegates and Datasource Methods in the ViewController itself.
That way you don't have to worry about accessing the ViewController containing the UITableView.

Controlling a button text from another view

I have two views and i used two view controllers. From the main view i want to initialize the button in the second view with a certain text.
If I press the button in the second view, then i can store that button reference to a global UIButton, and change the text then onwards.
But the very first time, how can I initialize the button text in the second view from my main view with a particular text ?
You should not do that: every controller should be responsible for its own view. Full stop.
You can always set some poperty on the second controller and use it when the second view appears on the screen to change whatever you like.
you use delegates & #protocol to access other class example,
refer ex, ex1, link
Not sure how you did this. But if you post some code, it will be helpful. If you already have the above code working, then you need to have the following in order for what you are expecting to work.
Say you have ViewControllerA, ViewControllerB as the two view controllers. You can definitely initialize the text in the button when you create the XIB file.
But I am assuming that you want to do this programatically. If you want to access a button in another view controller, you need
a. Access to the other view controller.
b. Once you have access to the other view controller, access to the UIButton variable in that view controller.
So you can do the following in the ViewControllerA code,
-(void)changeButtonTextOfVCB{
viewControllerB.button setTitle:#"MyTitle" forControlState...
}
and you can have a button in viewControllerA, which will trigger the above method.
Having said this, I am not sure whether you have the whole thing working, where you can even switch between the view controllers
If you make a button in FirstViewController and want to change button properties like title..etc from the second view, then you should make properties in SecondViewController
#property (nonatomic,assign) UIButton *button;
when you navigate firstViewController to secondViewController just pass the button Object like
obj = [SecondViewController alloc]...]
obj.button = button;//(FirstViewController button object)

Custom view controller help

I'm a bit confused as to implementing custom view controllers. I have a view that I want to have slide down from the top of the window. The view has three buttons on it. When the button for the view to drop is tapped the view drops. And when tapped again the view slides up/goes away. I have the drop down view saved as a nib file. Would this be the best method for implementation? Or should I have the view in the main view's nib?
And could I get some direction on how I should set it up?
The usual pattern has each of the view's stored in their own XIB file and associated with their own view controller objects. You then alloc/init the new view controller and point it to its XIB and present it modally. Once its presented, its VC responds to its actions and interacts with the model and updates its own views. You can then dismiss that view controller and its views to revert back to the parent view controller.
I have noticed a pattern mentioned in SO where people alloc/init a child VC and then within their present VC they addSubview the newVC.view, but that just seems pretty unusual to me.
If you just have a subview that is being animated down to partially cover the screen, perhaps it doesn't warrant its own VC since, as I think I'm understating your usage, its actions would map to your current VC. In that case, I would either create its contents programmatically or just as another view in the XIB for your first VC and animate that down when needed.