Quote:
Originally Posted by iant
I'm trying to create a new view on a button press and copy the text from one text field into the other.
The view gets created and displayed but if I try copy the string it crashes with:
"-[DistanceViewController ftCandles]: unrecognized selector sent to instance 0x4e44330"
Code:
mySecondViewController = [[DistanceViewController alloc]
initWithNibName:@"mySecondViewController"
bundle:nil];
mySecondViewController.ftCandles.text = FtCandles.text; // this crashes
The data in FtCandles.text is good.
I'm guessing I have to do a cast, or the initialization isn't ready?
Sorry if this is moronic.
Any ideas?
|
What you are trying to do is very, very bad practice.
In the
MVC design pattern, only the controller object should interact with the view object. In iOS, the view
controller is the controller object. The UIView objects that the view controller maintains are the view objects. (Makes sense, right?)
If you want to pass a value from one view controller to another, add a string property to the target view controller. Then, in the viewWillAppear method for your second view controller, install the text in whatever view you want to use to display the value (or not, as your view controller sees fit.)
There are lots of reasons to do it this way. First, the target view controller's views may not exist at the time you are trying to do this. A view controller's views only get loaded after the view controller is displayed, and they can go away at any time if the view controller is not the front view controller (the system will unload a view controller's views if it runs low on memory.)
The more subtle, but more important, reason not to muck with another view controller's innards has to do with good design. If one view controller mucks about inside another view controller, those two view controllers are now tightly bound to each other. Any change you make to the target view controller may break the other view controller, and require you to go back and make changes. That's bad. Very bad. The end of that road is "spaghetti code." Spaghetti code is a big mess, all tangled together. As the app gets bigger and more complex, it gets harder and harder to figure out what's going on, or make changes.
This concept is called encapsulation, and it's a key part of object oriented design.
When you're designing an app, think about giving your classes a fairly high-level, clean public interface that is based on the job they do in your application, not the messy details of HOW they do that job. Then write your classes so the details are all internal to the object. That way, you are free to change the internals of any class to your heart's content, and as long as the public interface is the same, the other classes in your application are not affected.
Another HUGE advantage to this approach is code reuse. If you design your code to be modular, you can pick up a class from one application and drop it into another application and it works.
Ok, lecture over.