UINavigationController issue - ios

In my app I'm switching between UIViewController using Navigation Controller and the Action Segue "Show". But for example if I write something in a UITextView and I return in that UIViewController the text is deleted. How I can keep the information ? Like in the UITabBarController.

You can create uiviewcontroller objects in app delegate and use that objects for swapping between view controllers. In current scenario, whenever you load same view controller again, it is created as fresh object so UITextView is initialized again, so text you entered will not be saved.
Or you can save value of UITextView in global variable or NSUserDefaults on viewwilldisappear() and initialize again in viewwillappear()

U can use restoration ID in the IB for that particular UITextView.

Related

Access UITabBarController to assign action via Storyboards

Without storyboards, I can simply assign the UITabBarController.delegate = self (in AppDelegate, for example). This lets me handle what happens when a UITabBarButton on the UITabBar is pressed.
However, I'm now using storyboards, where my UIViewController is contained in a UINavigationController, which is contained in a UITabBarController.
What's the best way to access this parent UITabBarController in code? I'd like to do this so I can assign a delegate to it. When the user presses a button in the UITabBar, I want to take an action in my contained UIViewController.
self.navigationController?.tabBarController

IOS: What is the correct way to change a view from the class of a different view?

I have a UIImageView subclass (in swift) set up so I can access touchesBegan/touchesMoved/touchesEnded.
When one of these methods is called, I need to change a property of a different, loaded view.
It seems to me that I will now need to access the active view controller in order to set the properties of this other view. Is there are better way to go about this (such as event methods called in the view controller)?
Note that I'm new to iOS and I am not extremely familiar with the event system yet, as most information I've found is written in Objective C and not in Swift. (Don't worry, I'm looking through Apple's Documentation.)
Also, no, I can't change the UIImageView to a UIButton. Even if I changed to a UIButton, I need access to the individual touchesBegan, etc. methods and the same problem would persist.
If I'm understanding you correctly, you need to reference the UIViewController from the UIImageVIew to push/present a new UIViewController. You have a few options:
Fire an NSNotification from the image view and have an event listener on the view controller.
Create a delegate on the image view that fires a selector on the view controller.
Hold a reference to the view controller on the image view, and push a new view controller from the image view with that reference.
Modally present a new view controller on the application window's rootViewController.

Using storyboarding in xCode how can I change the value of a label from a different view?

I'm writing an app that uses storyboarding and I want to update the labels in one view by clicking a button in a previous view.
_label.text = variable1;
is the line I would use to change the value of label, which is in the next view, when I click the button. Using this method I can easily change labels in the same view as the button but it does nothing when I go to the next view and see empty labels.
I've tried looking everywhere and found similar issues but couldn't find anything that worked for me so any solution would be very appreciated!
Unfortunately, it is not possible to connect IBOutlets between different scenes in storyboard.
It is difficult to suggest some precise solution because you have to provide more details about the setup which you have. Still, it is possible to outline some possible solutions:
Using prepareForSegue
If the view controller which you want to modify appears after the segue is performed you can customise its appearance in prepareForSegue function.
Using delegation
You can assign the view controller which wants to modify another view controller as its delegate. For example, if ViewController1 wants to modify ViewController2:
#interface ViewController1: UIViewController {}
#property (nonatomic,weak) ViewController2 *controllerThatIWantToModify;
with such setup you can call:
self.controllerThatIWantToModify.label.text = variable1;
You use storyboards, so there must be a segue from your first viewController (with the button) to your second (with labels in it).
If it is the case, you can set up the labels of the second view controller from the prepareForSegue method of your first view controller.
This method is called with a segue object which has a destinationViewController property which is your second view controller.
If you have several segue from this viewController, you should check if it is the right segue and then set it up.
To do that you need to set up outlets that gives you access to the labels from the viewController.
Then you can either write a setUpLabelsWith:(NSString)text1 ... method in your view controller, or directly access the outlets from the first view controller (supposing their are not private).
Yes this supposes your second view controller has a custom class.

Reuse childs from custom UIVIewController using storyboard

I have a storyboard with a navigation controller that leads to an UIVIewController that I want to reuse. That UIVIewController has a ParentUIViewController that has all the basic functionalities for all the UIVIewControllers that I am reusing.
Currently I am copying and pasting (meh) and then I change the class of the UIViewController to the ChildUIVIewController that I want to use (ChildUIViewController extends ParentUIViewController).
But this sounds like a bad solution. Everytime I want to change the ParentViewController visually I need to update, manually, all other ChildViewControllers.
I have tried to create a xib for the ParentViewController but the xib isn't loaded because I need a xib with the name of the ChildViewController. I have created it and then said the class is the ParentViewController but it crashes in the segue.
EDIT
I have created an example of the status of my problem
https://github.com/tiagoalmeida/storyboardexample
Note that the ParentViewController has a set of logic way more complicated that is not illustrated there. Also note that I am also using a TableView. I hope that this can illustrate the problem.
Keep the logic on the parentViewController and the UI Part on the child UIViewControllers. If you need to create a new UIViewController, you will create a child that will have a corresponding XIB (or get rid of XIBs and create the interface by hand).
Have you considered looping back into the same UIViewController via a "phantom button"?
Have a look at this: UIStoryboard Power Drill, Batteries included
Essentially you can drag a Bar Button Item into the little black bar under the View Controller in Storyboard (the 1 with View Controller, First Responder, and Exit icons; sorry, I don't recall what this is called exactly), then you can control+drag from that button back into the UIViewController for a Push segue. This should create a loop segue in your Storyboard. All you need to do next is give that segue an identifier, programmatically call it from your code using [self performSegueWithIdentifier:], then implement -(void)prepareForSegue: and use [segue destinationViewController] to conditionally set the title and perhaps some flags so you can identify when to use different kinds of fetches (or other code variations) in the same Class code.

Data sent back to Uitableview

I have a UITableviewController and I push another UIViewController in 'didSelectRow..' method.
I have user input controls (combobox, stepper) in this viewController, that when the UIViewController is popped , I would like to receive the newly entered data in the UITableviewController (and update the tableview accordingly).
I saw some questions/answers, and some said to use "Delegation/Protocol" approach, but did not find any specific example how to achieve this.
Can someone help?
Create a new file for your project and choose the Protocol file type. (We'll call it CallBackProtocol.) In the view controller that you push, create a property that has a type of id<CallBackProtocol> delegate;. Have your table controller adopt the protocol and, when it creates the view controller, set controller.delegate = self;.
Define a method in the protocol that lets you pass whatever data you need back to the caller. Implement that method in the table controller and call it from the second view controller just before popping it.
(Or use a NSNotification.)

Resources