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 06-10-2011, 10:43 AM   #1 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default Is this good programming pratice?

What i started doing is with view that would need to manipulate data in, i started placing an instance of the class or view controller in the next class, then i would use the assign property in order to change or manipulate any data from the previous class by just calling the object in the new class and changing the data accordingly. Ok so let me get to the example now.

NOTE:This is not exactly how the controllers work they are more complex in data and in operations im only trying to give a short example

heres view controller 1's header file that places another view upon it

Code:
 
@interface FirstView : UIViewController{
    SecondView *secondView;
    UILabel *label;
    
}
@property (nonatomic,retain) SecondView *secondView;
@property (nonatomic,retain) UIlabel *label;
;


now somewhere in the .m file for FirstView the user would press edit which would bring up another view with a textfield.
Code:
@interface SecondView : UIView{
    
    FirstView *firstView;
    UITextField *tf;
 }
-(void)updateLabel;
@property (assign) FirstView *firstView;
@property (nonatomic,retain) UItextField *tf;
;

I would then in the init method set the firstView variable to the previous view which is loading this view on the screen

now in the SecondViews.m file i would have a function like this
Code:
-(void)updateLabel{
      [firstView.label setText:tf.text];
      //do some functions that remove this screen
}
I would like any input some of you can give me on this like pro's con's of using this type of programming method. Or even a better option in trying to do the same functionality,
Thanks

Last edited by Esko2300; 06-10-2011 at 11:50 AM.
Esko2300 is offline   Reply With Quote
Old 06-10-2011, 11:23 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You should never manipulate a view of one controller from another controller.
baja_yu is offline   Reply With Quote
Old 06-10-2011, 11:49 AM   #3 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
You should never manipulate a view of one controller from another controller.
Its not another view controller its a view i lie upon the existing view controller thats another object of UIView.
I should have changed it in the second view .h file sorry
Esko2300 is offline   Reply With Quote
Old 06-10-2011, 12:59 PM   #4 (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 Esko2300 View Post
Its not another view controller its a view i lie upon the existing view controller thats another object of UIView.
I should have changed it in the second view .h file sorry


Not true.


Code:
@interface SecondView : UIView{
    
    FirstView *firstView;
    UITextField *tf;
 }
-(void)updateLabel;
@property (assign) FirstView *firstView;
@property (nonatomic,retain) UItextField *tf;
In your secondView code, you have a line like this:

-(void)updateLabel{
[firstView.label setText:tf.text];
//do some functions that remove this screen
}

The method updateLabel is directly manipulating a label inside FirstView, which is a view controller. That is VERY, VERY BAD programming practice. Outside objects should never read or write data from a view controller's views directly.

Instead, create a string property (lets call it labelText) in firstView. From outside, change labelText string property. Then, inside first View, use the value of the string property to set the label.

Update the label's text immediately if the view is currently being displayed. If not, just save the new value, and assign it to your label your view controller's viewWillAppear method.
__________________
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 06-10-2011, 02:53 PM   #5 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

@Duncan

Ok so since your telling me i should just have a variable,in the FirstView called Label TExt, that i will change directly in the SecondView by assigning it the value from the textfield in the secondview. I think it would be better that i just create a method in the first view that takes in an nsstring and sets it to the label in the firstview.

so in my firstView.m ill have a method
Code:
- (void)updateCellText:(NSString*)text{
     label.text = text;
}
but i should call it from the variable i assigned the FirstViewController to in the SecondView like so

in my secondView.m
Code:
- (void)changeText{
      [firstView updateCellText: tf.text];
}
The point of me asking this was should i use the assign property when i need to do something like this. And from what i read you said that its pretty much ok to do this just dont manipulate any UI in another Viewcontroller. Am i putting the pieces together correctly?
Esko2300 is offline   Reply With Quote
Old 06-10-2011, 04:06 PM   #6 (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 Esko2300 View Post
@Duncan

Ok so since your telling me i should just have a variable,in the FirstView called Label TExt, that i will change directly in the SecondView by assigning it the value from the textfield in the secondview. I think it would be better that i just create a method in the first view that takes in an nsstring and sets it to the label in the firstview.

so in my firstView.m ill have a method
Code:
- (void)updateCellText:(NSString*)text{
     label.text = text;
}
but i should call it from the variable i assigned the FirstViewController to in the SecondView like so

in my secondView.m
Code:
- (void)changeText{
      [firstView updateCellText: tf.text];
}
The point of me asking this was should i use the assign property when i need to do something like this. And from what i read you said that its pretty much ok to do this just dont manipulate any UI in another Viewcontroller. Am i putting the pieces together correctly?
You're on the right track.

However, you should be aware that unless a view controller is the front view controller, it's views may not exist at all. The outlets will be nil and the views won't be loaded until the first time the view controller has been loaded, and then the views can be unloaded again by the system at any time (if the view controller isn't the front view controller) to release memory.

Even if the label is loaded at the time you try to assign the text, it could get unloaded in the future if the system needs to free up memory. Saving data in UIView objects is not a good idea. Save the data in your model, and then PRESENT the data in your view objects.

Thus, you should not take the string that is passed to you and try to assign it to your label directly. Use a retained or copy property to store the value. Then assign it to the label in case the view is the front view.

Also assign the text to your label in your controller's viewWillAppear method. That way, when you display your view controller, the label will be set up with teh correct text.
__________________
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 06-10-2011, 04:21 PM   #7 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

Thanks i see what your getting at in my future programs ill try and do things differently. Right now this works and even though it aint the best its working as i need it too.

Another quick question though. The Model-View-Controller aspect of things.
Model would be the data with in the view i.e. all my strings, floats, ints ect.
The View would hold the UI variables.

But im not understanding the Controller aspect of it since when you create a new view controller it gives you a view and a controller. how are these separate from each other?
Esko2300 is offline   Reply With Quote
Old 06-10-2011, 04:27 PM   #8 (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 Esko2300 View Post
Thanks i see what your getting at in my future programs ill try and do things differently. Right now this works and even though it aint the best its working as i need it too.

Another quick question though. The Model-View-Controller aspect of things.
Model would be the data with in the view i.e. all my strings, floats, ints ect.
The View would hold the UI variables.

But im not understanding the Controller aspect of it since when you create a new view controller it gives you a view and a controller. how are these separate from each other?
Your code has a latent bug if you are setting labels to a string and then forgetting about that value.

Consider this:

While View controller A is the front view controller, it invokes the setLabelString method in view controller b to pass a new string in.

View controller B obediently sets the label string, and it seems to work.

The user goes back to view controller b, and everything seems fine.

Then the user goes to view controller c and does something that needs more memory. The system triggers a low memory warning. View Controller C is the front view controller, so the views for view controller a and view controller B are released.

The next time you invoke view controller B, it will reload it's views from its nib file, and your label text will be lost.

You can trigger this bug at any time in the simulator by switching to a view controller other than view controller b and selecting "trigger memory warning" from the simulator's hardware menu.
__________________
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 06-10-2011, 04:41 PM   #9 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Your code has a latent bug if you are setting labels to a string and then forgetting about that value.

Consider this:

While View controller A is the front view controller, it invokes the setLabelString method in view controller b to pass a new string in.

View controller B obediently sets the label string, and it seems to work.

The user goes back to view controller b, and everything seems fine.

Then the user goes to view controller c and does something that needs more memory. The system triggers a low memory warning. View Controller C is the front view controller, so the views for view controller a and view controller B are released.

The next time you invoke view controller B, it will reload it's views from its nib file, and your label text will be lost.

You can trigger this bug at any time in the simulator by switching to a view controller other than view controller b and selecting "trigger memory warning" from the simulator's hardware menu.
OK i see it now. But in this situation its ok since ill never need to know the value of the string because it gets sent to the server. When i come back to this view i just grab a list of names from the server again, display it on the screen and the user can choose from a list of names.

and any input on the last question i posted? cause clarifying that up for me would be great.

Last edited by Esko2300; 06-10-2011 at 04:44 PM.
Esko2300 is offline   Reply With Quote
Old 06-10-2011, 05:09 PM   #10 (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 Esko2300 View Post

Another quick question though. The Model-View-Controller aspect of things.
Model would be the data with in the view i.e. all my strings, floats, ints ect.
The View would hold the UI variables.

But im not understanding the Controller aspect of it since when you create a new view controller it gives you a view and a controller. how are these separate from each other?

I didn't answer this part of your question.

In MVC, the model holds the data. The view is used for display, and the controller mediates between the two.

The view controller is the controller object. The view hierarchy of the display is the view. The model can be as simple as an array that's fed to a table view, or even NSString objects that hold the values displayed by the labels in your screens.

For simple projects, the line between model and controller can get blurry. That's not a big problem because the controller object is in charge, and manages how to store it's data. It pays, though, to remember what part of a controller object's data is really model data that needs to be saved.

The idea is that you always go to the controller object in order to make a change in an MVC "triad". The controller has the smarts to decide what to do with messages, when to flush data changes to disk, etc.

Trying to save data in your labels is using the view (UILabelView) to store data. That's bad. Storing data is the model's job.

Trying to change one view controller's label from another is bad, because you are bypassing the controller object. Again, the controller is the brains.

Consider this analogy: If I go into your desk calendar and add new entries, move appointments around, etc, but don't tell you about it, you're going to be confused, and might miss appointments as a result. The desk calendar is your model object, and you are the controller. Or lets say I'm the ad agency for your company, and I decide to completely change around your ad campaign. Different slogan, different logo, and airing your ads in different markets. Again, I don't tell you. Your ad campaign is your business' view. It's the visible part of your business. If I change your ads without consulting you (the controller) then the view might not reflect what's really going on in your business.
__________________
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 06-14-2011, 08:56 AM   #11 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

Short wrap up of what you said. The model = data. The View = The layout of the screen. And the View Controller controls everything. So the View controller should have objects of each model and view objects that correlate with each other. One view controller can have 5 model objects and 5 view objects,which is how apples intended it to be, where all their UI interactions and changes in the data between these objects are controlled by the view controller
Esko2300 is offline   Reply With Quote
Reply

Bookmarks

Tags
sharing data, uiview, viewcontrollers

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: 317
7 members and 310 guests
chemistry, Dnnake, iOS.Lover, jenniead38, lendo, Leslie80, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

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