views:

65

answers:

1

Can someone post the simplest example on how to use controlTextDidChange with a text label in the .h and .m files? Another part of my code constantly changes the value of this text label multiple times a second. I just need another part of my script that can tell if the value has changed and preform an action. All these websites out there that have this topic don't cover how to FULLY use it. So I thought I would ask it here! :D

Thanks! Elijah

+5  A: 

It sounds like you're taking a pretty inappropriate approach to your problem. Specifically, you're using a control to store data, rather than storing it separately in your model and interacting with that model from the rest of your application.

I think you need to take a step back and do the following:

  1. Learn the principles of object-oriented programming and the Model-View-Controller pattern, particularly as they apply to Cocoa. One of the best introductions is the book Object-Oriented Programming with Objective-C, online free from Apple.

  2. Plan out a bit of the structure behind your application - how the data it works with fits together, not just how the user interface will look. Your application's user interface and the data it works with are related but not the same.

That will probably help you get the "big picture" for your application right, and show you more easily how to put together the pieces like "do X when this value changes" and "put the value for that in a text field" than trying to learn only the pieces you think you need as you think you need them.

One more thing: Your application is an application, not a script as you referred to it in your question. This is important to keep in mind: An application can have quite a few moving parts. Tools like separate classes, separate .h and .m files, and so on are ways of managing these parts and keeping them as independent from each other as appropriate.

It's also not just a sequence of "commands" that are executed one after the other as a script might be. Instead, the interactions between the parts of an application are managed by the Cocoa framework, according to its application lifecycle. The Application Architecture Overview and the Cocoa Fundamentals Guide are good to read in order to get a grounding in how Cocoa fits together with your own code when building applications.

Chris Hanson
Ok, thanks alot! Can you show an example of something that might work in my case? This is a quick project that I need. I will have to read up and all that when this is done. I know, probably not the best way to do this, but I just have to do it this way once. :D Ok, so this is what I'm doing: I'm using a Quartz composer view (QCView) in interface builder. This quartz composer is the music visualizer template. Then I'm using a patch that sends the sound output to a NSTextField (label). I need to get that label data and compare it to other variables. Any ideas on how to do this?
Elijah W.
BTW...what I was thinking with the controlTextDidChange: Is that when the value of the NSTextField(label) changed values it would grab the number and store it in a variable I can access in my code.
Elijah W.
My point in my answer is that you will not have good luck with this approach to writing software, regardless of whether it is "a quick project that [you] need." Providing examples of the wrong way to solve this problem will not help you learn, and will definitely not benefit others coming to this page in the future (via search engines) trying to figure out the right way to do something.
Chris Hanson
Also, -controlTextDidChange: is unlikely to help you in the case where the text in a control is changed programmatically. Usually these kinds of methods are invoked when the user manipulates a control, not when code does, because code can just tell other code about such changes directly.
Chris Hanson
Ok...thanks for your help! I'll definitely read that ASAP. But can you just quickly tell me what I would need to get this working? :D
Elijah W.
No, because there is no simple way to get what you want ASAP. Poor application design makes everything harder. Perhaps have your patch send the sound output to a model object instead.
Chris Hanson
ok, thanks!______
Elijah W.
Elijah Wood: He speaks the truth. I know it can seem “easier” to just stuff all your data into controls and make a big-ball-of-mud class to hold all the logic, but that just makes your app into a Tower of Babel that *will* fall over and cause you to swear in many languages. Save yourself the confusion *and the time!* and start factoring your application appropriately now. It really does make everything much easier.
Peter Hosey