Ok, I know that the model for the new graphing vc is going to be a subclass of the calculator brain, but how do I get the program stack of the CalculatorBrain from the old VC to the GraphingBrain of my new VC? DO I have to go down the stack on the Storyboard-segue?
I have the pinching and panning on the new axes setup, as well as the delegate to communicate between the new graph brain and graph vc. But isn't my programStack that the user typed in before "trapped" in the old MVC?
I think I've got the concept down, but I don't see the way for that communication to occur.
Thanks in advance!
***For those who don't follow the course, I have two MVCs linked by the NavigationController. The user enters a series of numbers and operations (in the first MVC) which are stored in are then stored in an array in the model of the first MVC. When a button is pressed, it segues to a new MVC, which needs the array that was entered in the previous MVC. How do I transfer that array between two seemingly separate MVCs? I don't think I can have a property in the second VC and set it from the first VC because it(the second MVC) is not instantiated yet. It maybe something with preparteToSegue but I'm not sure.
Having gone through CS193P (well more than half way there), I think I understand your questions. Here is what you need to remember:
To send a property value down the MVC (C --> V) path and in this case the V holds another MVC; use PrepareForSegue. Regardless of whether the destination has been instantiated or not, you need to do a few things:
Import, in the .h or .m file header of the 1st class, the 2nd class.
In the prepareForSegue, create an instance of the 2nd class and set it to be equal to segue.destinationViewController. You will need to typecast the latter to the second class.
Now you can set the property of this instance you created (of the 2nd class) to any value you want.
You cannot set any IBoutlet values for the second class, from the prepareForSegue, because IBOutlets are not setup until after ViewDidLoad.
For some odd reason, if you do use a condition to check for the segue.identifier, you will need to enclose the statements in {} even if it is only one statement.
Hope this helps and gets the juice flowing...
KMB
Related
It is possible, to create an exact object copy of a UINavigationController? I have seen examples of copying objects using copyWithZone:, but I am confused as to how I would use this to copy my UINavigationController.
Any help?
UINavigationController doesn't conform to the NSCopying protocol, so you can't use copyWithZone: or copy on it.
If you are looking to have a customised UINavigationController that you can use throughout the app then you should subclass it and then create a new instance of that subclass every time you need a new one, such as when you create a new modal view controller.
EDIT: If you want to keep the view controllers from a previous navigation controller then you can do something like this (use subclassed navigation controller if needed):
UINavigationController *newNavigationController = [[UINavigationController alloc] init];
[newNavigationController setViewControllers:oldNavigationController.viewControllers animated:NO];
This will do a shallow copy of the viewControllers, i.e. you will have references to the original navigation controller's view controllers, not copies. If you want to do a deep copy on the view controllers then that will be far more complicated and will require specific copying code for each view controller. (See here for more info).
You can do this by creating a category (or a subclass), make the category NSCoding compliant, and add the necessary encoding and decoding functions. You then need to determine what properties you want to encode - the types of view controllers it currently has in its array, and perhaps you'll need to make those objects be NSCoding compliant. You can see that this is not going to be a trivial thing to do, but its not impossible. You may find the solution to your problem is best done using some other techniques.
EDIT: If you want to "duplicate" it, what you really need to know is what viewControllers are in the array. So suppose you want to replicate "state", which in some sense is the same as the original answer but less rigorous. Add a category or method to each object and ask to to give you current state as a dictionary. For the navigationController, that might be just the classes of the objects currently on the stack.
For each of these objects on the stack, you get them to give you a dictionary of their state. By state, its means what text is in UITextFields, views etc, anything that that object would need to go from a startup condition and get back to where it is now.
You package this all up - the nav dictionary and array of the state ones. You can save this as a plist. When you want to construct where you were later, the nav controller can tell what objects to create by knowing their class, then as each one is created it can be sent its dictionary and told "get back to where you were". Once done, then push another controller on the stack.
Let's say I have a two subclasses of UIViewController called MasterViewController and DetailViewController.
DetailViewController has a property of type NSNumber called level and a UILabel called levelLabel.
MasterViewController has a segue to DetailViewController called ToDetail. MasterViewController's prepareForSegue is like so
- (void)prepareForSegue:(UIStoryboardSegue)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ToDetail"]) {
DetailViewController *detailVC = (DetailViewController *)segue.destinationViewController;
detailVC.level = [NSNumber numberWithInt:10]; // never mind the literal...pretend there was some algorithm for it
}
}
So then, in DetailViewController we implement the setter for levelLabel like so:
- (void)setLevelLabel:(UILabel *)levelLabel
{
if (levelLabel) {
_levelLabel = levelLabel;
_levelLabel.text = level.stringValue;
}
}
Is this good code design? Also, could you critique my code writing style? I pretty much wrote all this code on the fly so this is pretty much how I write code for the most part.
I thought of this question while showering because this is how I implement the setting of almost all the label texts that depend on a segue.
What follows is my own way of thinking about such relationships. Italics applies to your question.
You have the thing being controlled (the label) the controller (destination view controller) and the context it is being controlled within (the source view controller). This can also be expressed as model-view-controller, but I think thinking about a context can apply to much more specific and localised situations.
You should generally try to keep information flow going in one direction, from the context downwards. Objects should not have to be aware of the context in which they exist, ie they shouldn't have to ask for any information, they should be told everything they need to operate. So the source view controller should push the level to the destination view controller, the destination view controller should push this information to the label. This is what you already have, sort-of.
To build upon the above, not only should information flow in one direction, but I also try to ensure the relationships are causal, ie pushing information from one object to another should cause it to subsequently be pushed to the next object. Your code is not doing this which is probably why you have a bad feeling about it.
A more appropriate thing to do is set the text property of the label within the level setter, so that when you set or change the level, the label will update subsequently. The label may or may not be loaded so you will have to check whether it is using -isViewLoaded; -viewDidLoad is the appropriate place to set the text property upon first load.
(When I say 'push' that's just my way of thinking about setting properties or passing arguments because it implies directionality. It is really dependency injection. An example of pulling information would be delegates and data sources. But note here still the object isn't aware of any context, delegates and data sources are clearly defined as protocols, not classes, and usually within the same header file, and are themselves pushed onto the object from a surrounding context. So yes the object is asking for information, but on its own terms and from a system it has no knowledge of.)
Re coding style:
That's exactly how I write code but note Apple reserves the use of underscore prefixes
I'm going through my code to make sure that all of my properties have the proper weak/strong modifier and I ran across this situation which I don't know what to do with. Keep in mind that I'm fairly new to iOS programming.
I have a normal MVC hierarchy where my controller creates an object called FieldManager. This FieldManager is used by my controller to dynamically create textfields. However, this FieldManager also needs to be used by the controller's model to periodically query the manager to find out information about the fields (such as is it required, should it's text be capitalized...etc).
So to summarize, I have a controller which creates an object that is used both by the controller and the controller's model. Therefore, I don't know if I should make the model's reference to FieldManager a weak property or the controller's reference to it a weak property. It seems that I should make both weak properties, otherwise FieldManager will get released. What should I do?
Thanks.
Things like that should belong to your model, so the way to go is to have a datasource.
Your controller asks the datasource to create and return the textfields, the datasource contacts the model and asks for a field manager for that model.
That's the way I would do it...
I'm curious if it's possible to create an application according to window-based pattern, add UILabel, create a new class, and to be able to change UILabel's value from this new class.
After creating all that files we will have:
NewApplicationDelegate.h, .m;
Newclass.h, .m;
MainWindow.xib.
The IBOutlet in this case must be added to MainWindow.xib, and I want to change its value from NewCalss.m. Is it possible? How can I do that?
The point is that I was working only with navigation-based or simple window-based applications before, and now I need to have one view available and changeable (UILabels, for example) from at least 2 other classes.
Thanks.
You will have a UIController object too - right?
That has a handle on the UI elements, UILabel etc, whichever you map through.
You could pass this to the Newclass or you could provide an interface/protocol that the controller exposes that allows Newclass to interact with what you want to do.
Working on cs193p (stanford online videos of iphone programming), I am working on Presence1 exercise
I am trying to change a UIView which is a subview of another view dynamically.
For those of you who are not aware of the assignment posted on the course, Basically the architecture of the app is as follows:
Nav Controller -> (Root) View Controller - (VC)1 for 1st screen -> calls Detail View controller VC2 for next screen
Now 1st screen has images and text that I want to load dynamically from my model (which is an object I instantiate in App delegate before pushing the 1st VC on navigation stack. I have defined a parameter in initWithNibName method to pass this model object while initializing the nib for VC1. And I also try to set the image from the model over here and in viewDidLoad and viewDidAppear methods. Its not working.
When I debugged, I saw that the model object being passed is empty.
If you understand the problem, please lemme know what am I missing.
I can post the code if required but will have to post the whole thing to make any sense.
Nailed - I was releasing my model object when not required - I was creating a "reference" to one of my objects present in the model (array containing a list of objects) . Note - I was not creating a new object by using alloc/copy. I was then releasing it (even though after the push on to the navigation stack) - Obviously causing my object memory space to get cleared and that reflected in my view controller that was receiving that model object as a parameter in its init method. This was giving the illusion that the object is not getting passed to the view controller!
Lesson Learned earlier - Be very mindful about releasing objects.
Lesson Learned Today - Be very cautious when releasing objects. Don't over release objects - make sure you are releasing an object only if you are calling alloc/copy or retaining it explicitly - memory management 101 - revisited :)
Thanks all for not replying to this question. In a way it forced me to scrutinize my code at a very micro level and I am sure I wont forget this for rest of my life! :)