iOS passing values from two level Modal view controller - ios

I'm trying to make a form that one of the filed takes value from a two level selections' result.
The main progress will something like:
EditViewController ===> CategoryViewController (which embedded inside a NavigationController by storyboard and popped up as a modal view) ===> SubCategoryViewController (Which will be pushed to NavigationController).
Now I have a problem. After user tap to select a value in SubCategoryViewController, I'm supposed to dismiss SubCategoryViewController and return the value to EditViewController. But I don't know exactly how.
Please suggest any solution.
Thank you.
EDIT:

Every one of those view controllers should have a public property for a weak reference to a model object that represents whatever is being edited.
So every ____ViewController.h file would have:
#property (weak, nonatomic) CustomItem *item.
in its interface (assuming a strong reference is somewhere in some data store or array of all the items).
When EditViewController is preparing for the segue to show CategoryViewController modally, it should assign that same reference to CategoryViewController's item property after assigning any data entered in EditViewController's form to item:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//TODO: assign data from controls to item, for example:
//self.item.title = self.titleField.text;
CategoryViewController *vc = (CategoryViewController *)segue.destinationViewController
vc.item = self.item; //pass the data model to the next view controller
}
Likewise for the segue from CategoryViewController to SubCategoryViewController. This ensures that every ViewController is editing the same object in memory. When you dismiss SubCategoryViewController (assuming somewhere in all of this CategoryViewController was already dismissed), viewWillAppear: will be called on EditViewController- there you can refresh any changes made in the modal views to the item property, just like you would when first displaying the view (it's actually the same method that's called):
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.titleField.text = self.item.title;
self.categoryLabel.text = self.item.category;
self.subcategoryLabel.text = self.item.subcategory;
//etc....
}

Related

Passing Bool Value from one viewcontroller to another in ios [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 6 years ago.
I am trying to send a BOOL value from
ViewController A to ViewController B.
On ViewController A, dashboard.skip shows YES, but on
ViewController B, self.skip shows NO.
- (IBAction)skipToDashboard:(UIButton *)sender {
ViewController B *vc=[[ViewController B alloc]init];
vc.presentButtonTag=sender.tag;
self.fromSkip=YES;
vc.skip=self.fromSkip;
}
In ViewController B:
#property (nonatomic,assign) BOOL skip;
There are 2 ways to pass your data from First screen to 2nd screen.
1st Way:
You can pass your data by creating VC object and assign value in it and push to next screen like below example.
Code :
//Initilize view controller from storyboard id.
ViewController *objViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
//Assign value
objViewController.presentButtonTag=sender.tag;
self.fromSkip=YES;
objViewController.skip=self.fromSkip;
//Push view
[self.navigationController pushViewController:objViewController animated:YES];
2nd Way:
By Storyboard you can set segue action on button and in prepare segue method you need to pass your data like this.
Code:
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:#"ViewController"]) {
ViewController *objViewController = (ViewController*)segue.destinationViewController;
objViewController.selectedTag = ((UIButton*) sender).tag;
//Assign value
objViewController.presentButtonTag=sender.tag;
self.fromSkip=YES;
objViewController.skip=self.fromSkip;
}
}
Now you can check your scenario in both condition I think you are pushing to next screen by storyboard so use 2nd Way.
Let me know if you need more help.
Try to define BOOL property and set value either true or false.
For Ex: Appdelegate declare property and then use
#property (nonatomic) BOOL fromSkip;

Writing to a dictionary from several view controllers

I'm stuck with a problem which I'm not sure how to solve. I have several view controllers which follows each other as the user populates them with data.
Before changing views I use an alert controller for the user to confirm correctness of data before pushing to the next view controller. However, here I need to capture the data but I'm only writing the data to Realm once all the data is gathered.
My question is this; how do I temporarily keep this data (and use some of them forward) until it's time to write to the database?
The easiest way is to create a property in each of the view controllers.
in the .h file, under interface
#property (strong, nonatomic) MyRealmObject *realmObject;
in the .m file, under implementation
#synthesize realmObject
Then, in your prepareForSegue method, you populate the copy local to your view controller, instantiate the destination view controller, and then pass the local Realm object into the property of the destination view controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"SegueName"]) {
DestinationViewController *destViewController = segue.destinationViewController;
destViewController.realmObject = self.realmObject;
}
}
This will pass a copy of your existing realm object to the next view controller

I can update a BOOL located in a second view controller using the prepareForSegue method but not a Label, why?

I have been trying to have a better understanding on how to pass data between view controllers and everything is make more sense but there is one thing I would like to understand better.
I came across this thread/tutorial here at StackOverFlow (Passing Data between View Controllers), I tried it and it worked as expected, but the one thing I don't know understand is why we can change a BOOL property located in the second view controller but NOT a Label. In other words if I add a second property and try to change the label in the prepareForSegue: method it doesn't work, why?
In section Passing Data Forward using Segue's I tried adding a second property for a label in the second view controller, right where isSomethingEnabled is, like this...
#property(nonatomic) BOOL *isSomethingEnabled;
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
Than in prepareForSegue: method located in the first view controller I did this..
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showDetailSegue"])
{
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled = YES;// this works fine
controller.myLabel.text = #"Hello"; // here, no error but it doesn't update the label
}
}
Why you can modify a BOOL property located in a second view controller but not a label? Can someone explain this a little bit?
Thanks a lot
As Grzegorz Krukowski said the UIViewController isn't loaded at that moment. You can set #property values and access them in the viewDidLoad method and setup your Labels accordingly.
First of all, you should not do this. You should treat another view controller's views as private. The reason for this is the concept of encapsulation. A view controller's views are part of the view's appearance, not it's API. If at a future date you decide to refactor the VC so that it uses the text string in question as the title in a navigation controller, you should be free to do this. If another view controller reaches inside your view controller and manipulates it's view objects directly, you must keep those view objects unchanged forever, or you run the risk of breaking the way the other view controller interacts with it. That's bad, and 6 months from now you won't remember that 3 outside view controllers manipulate your view controller's views, and when you forget and change your views and decide to move a piece of text to a different UI object, your app will stop working correctly.
What you SHOULD do is to add a string property to your ViewControllerB, and set THAT in your prepareForSegue. Then in ViewControllerB's viewWillAppear:animated method, fetch the contents of that string property and install it in your label's text property.
Then the string property becomes part of your view controller's API contract (its public interface.) By adding a public string property to your interface, you guarantee that you will accept string values to that property and that you will do the right thing with them.
Now, as to why it doesn't work. A view controller's view hierarchy is not loaded until it's displayed. At the time of prepareForSegue, the view controller has been initialized, but it's views haven't been loaded yet. All its IBOutlets are nil. (this is another reason why you should not try to manipulate another view controller's views BTW)
Your prepareForSegue method looks like this: (wrong)
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"showDetailSegue"])
{
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled = YES;// this works fine
controller.myLabel.text = #"Hello"; // here, no error but it doesn't update the label
}
}
Instead, your code should look like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"showDetailSegue"])
{
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled = YES;// this works fine
controller.textToShow = #"Hello"; //This is the change
}
}
Then, in your ViewControllerB's viewWillAppear:animated method:
- (void) viewWillAppear: animated;
{
[super viewWillAppear: animated];
//install the text from our textToShow property into the label
self.myLabel.text = self.textToShow;
}

Passing data back between View Controllers embed in UINavigationController

I have view controller embed in navigation controller with one property (nonatomic strong NSMutableArray *myData), when I am pushing second view I am also passing my array data to this view using this method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"secondView"]) {
SecondViewController *svc = [segue destinationViewController];
svc.myDataInSecondView = self.myData;
}
My question is: Why if I change any values in my myDataInSecondView array and than I will come back to first view my values in myData array are changed as well? I always thought that I have to use custom protocols and delegates to pass any data back to the previous view.
The way you're doing it is a very common way of sharing data between view controllers. The second view controller has a property that can be set by the first view controller. The property is a pointer, the same as in the first view controller. Both point at the same place in memory, so either view controller can change that same data.
If you don't want the second view controller to alter the data, set the property as copy.
#property (nonatomic, copy) MyData *myData;

UIView not Initialized from Storyboard Segue

I am attempting to perform a Storyboard segue from a UIbutton that is a day on a calendar to a DayViewController with info on that day. The currentTitle of the sender UIbutton should be passed to a UILabel in the dayView of the DayViewController. It successfully segues to the new VC and assigns the button title to a property therein, however the dayView (and the base view) of my DayViewController is not getting initialized (their addresses in the debugger are both 0x0) and I get a blank page. How can I get these views to initialize in this segue? (I thought views linked to a VC were automatically initialized when segued to?)
Here's the prepareForSegue getting called:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender
{
if ([segue.identifier isEqualToString:#"ShowDay"]) {
DayViewController *vc = segue.destinationViewController;
[vc setCurrentDayNumber:sender.currentTitle];
}
Here is setCurrentDayNumber in the new VC
-(void)setCurrentDayNumber:(NSString *)currentDayNumber
{
_currentDayNumber = currentDayNumber;
[self.dayView setNeedsDisplay];
}
Here is my view that is linked to DayViewController in the storyboard.
#property (nonatomic, weak) IBOutlet DayView *dayView;
Thanks in advance!
When you declare a property as weak, it will not "hold on" to the value unless someone else has a reference to it as well. When the new view controller is pushed, the old button goes away, leaving currentDayNumber as the only reference, so it automatically sets itself to nil. (This is what weak is supposed to do.)
If you want to keep a reference to it no matter what, use strong instead of weak and make sure to set _currentDayNumber to nil somewhere (like viewDidUnload) so that you don't retain it forever.

Resources