Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2011, 10:58 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 7
iant is on a distinguished road
Default Copying text between UITextField's

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?
iant is offline   Reply With Quote
Old 05-10-2011, 11:34 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

you're reading the exception message wrong. It saying that your new view controller doesnt have a property "ftCandles", or perhaps you forgot to synthesize it.
smithdale87 is offline   Reply With Quote
Old 05-10-2011, 11:35 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iant View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-10-2011, 11:36 AM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

+1
smithdale87 is offline   Reply With Quote
Old 05-11-2011, 06:20 AM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 7
iant is on a distinguished road
Default

Thanks Guys,
Yeah I figured it was a little hackie.
Duncan, a few questions.
The "viewWillAppear" method does not exist by default. Should I write it or is it safe to set the string in viewDidLoad()?

Also are you suggesting I add a separate string property (other that the text fields text?). I'm guessing this stores the string until its can be set from the view?

If I create the 2nd view on the fly from a button press the view controllers will need pointers to one another (this seems like bad practice for code separation and since pointers may be null). Is there a better way of doing this?
Should I just do the setting in the app delegate to avoid the views having to store pointers to one another?
Ian
iant is offline   Reply With Quote
Old 05-11-2011, 06:37 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

as far as your viewWillAppear method goes you can just drop that in no worries. I generally add mine after viewDidLoad but thats more so i can locate them easily when moving about my project. Simply go to the line you want to add it on and type:
Code:
- (void) view
You should find then that all of the optional and preset view methods will pop up for you to choose from, select viewWillAppear and hit enter. It is safe to do this and there is no reason you shouldnt. If Duncan suggests placing your code in viewWillAppear then he is probably right that there is where it should go. ViewDidLoad is different to viewWillAppear. I dont know the semantics but i think "Load" is done first when the app says it wants that view, it is only called once unless you remove it completely, so at times it can not be called as opposed to viewWillAppear, i believe this is called after viewDidLoad, but is called every time that that view appears on the screen. In essence if you unloaded your view then viewDidLoad will be called, if you moved away from your view without unloading it then viewDidLoad isnt called as the view is already loaded so you need to use "Appear" - At least that is my understanding of it.

Ill leave Duncan to address your other issues as i dont want to contradict him with something that is wrong :P
Meredi86 is offline   Reply With Quote
Old 05-11-2011, 07:09 AM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default Data container singletons

Quote:
Originally Posted by iant View Post
Thanks Guys,
Yeah I figured it was a little hackie.
Duncan, a few questions.
The "viewWillAppear" method does not exist by default. Should I write it or is it safe to set the string in viewDidLoad()?

Also are you suggesting I add a separate string property (other that the text fields text?). I'm guessing this stores the string until its can be set from the view?

If I create the 2nd view on the fly from a button press the view controllers will need pointers to one another (this seems like bad practice for code separation and since pointers may be null). Is there a better way of doing this?
Should I just do the setting in the app delegate to avoid the views having to store pointers to one another?
Ian

The standard view controller template should include an viewWillAppear method. The signature is like this:


Code:
- (void)viewWillAppear:(BOOL)animated
The reason you want to use that rather than viewDidLoad is that viewWillAppear gets called every time the system displays your view controller. The viewDidLoad method only gets called when the view controller's views are created (the first time the view controller is asked to display it's views, and any time thereafter if the views get unloaded from memory. Views usually get unloaded in low memory conditions, although you can also code your view controller to unload them manually.)

It's easier to create your view controllers at launch time. They typically don't take that much memory. It's their views and data that are memory-intensive. I'd suggest coding your view controllers, and then creating instances of each of them in your app's main nib file.

The cleanest way to share data across your app is with a data container singleton. Do a search on singletons to read about it. In brief, the idea is to create an object with properties for all the data you want to share in your program. You then create a class method with a signature like this:

Code:
+ (MyAppDataSingleton) sharedDataSingleton;

The implementation would look like this;


Code:
@implementation MyAppDataSingleton
static MyAppDataSingleton* _theSingleton = nil;

+ (MyAppDataSingleton*) sharedDataSingleton;
{
	if (!_theSingleton)
		_theSingleton = [[[self class] alloc] init];
	return _theSingleton;
}
@end;
Then, anywhere in your code you need to access shared data, you use code like this:

Code:
[MyAppDataSingleton sharedDataSingleton].someProperty

In your example, you could make the string that you're passing between view controllers a property of your data container. You'd then set it in one view controller, and use it in the viewWillAppear: method of your other view controller.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-11-2011, 11:05 AM   #8 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 7
iant is on a distinguished road
Default

Cheers Everyone. Some solid suggestions there!
iant is offline   Reply With Quote
Reply

Bookmarks

Tags
copying, string, text, uitextfield

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 344
8 members and 336 guests
iOS.Lover, lorrettaui53, MikaelBartlett, oztemel, pbart, PlutoPrime, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:00 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0