Re: Access object from another view

2010-09-21 Thread Steve Wetzel
I thought I had tried putting the setText statement after the addSubView but I 
went back and tried it again and it worked.  Thanks for the suggestion.

I cannot simply use IB to put the same label text in as the text label is 
calculated from user input.

Thanks!

Steve






On Sep 21, 2010, at Sep 21:6:03 PM, Quincey Morris wrote:

> On Sep 21, 2010, at 14:03, Steve Wetzel wrote:
> 
>> I could not get the 
>> 
>> [self.viewController1.label2.text 
>> 
>> or the 
>> 
>> [self view addSubview:viewController2.view];
>> 
>> to work for some reason.  Then it dawned on me that I could set the value 
>> right before I added the Subview!
>> 
>> [viewController2.label2 setText:label1.text];
>> [self.view addSubview:viewController2.view];
>> 
>> This works, although for some reason the label does not update unless I 
>> reload the view.  I will figure out what is going on there.
> 
> The usual cause of such behavior is the timing of NIB loading. There are 
> various points at which code can be executed (init..., awakeFromNib, 
> viewDidMoveToSuperview, etc), and it can be hard to keep track of the 
> correctness of your assumptions of what must have happened already.
> 
> In order to transfer the label successfully, you need both view1 and view2 to 
> have been loaded. Since NSViewController's 'view' method causes the view to 
> be loaded if necessary, you can be certain it's done *after* executing:
> 
>   [self.view addSubview:viewController2.view];
> 
> since that references both views. Did you try putting the other line:
> 
>   [viewController2.label2 setText:label1.text];
> 
> *after* the 'addSubview' line instead of before?
> 
> Is the label static in view1's NIB? If so, why can't you just use IB to put 
> the same label text in view2 and have no code at all?
> 
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Access object from another view

2010-09-21 Thread mmalc Crawford

On Sep 21, 2010, at 2:49 pm, Steve Wetzel wrote:

> Then [self.view addSubView:viewController2.view]; brings up the subview.
> 
You should [almost certainly] not be doing this.
If you want to display another view controller's view, you should use an 
appropriate technique to present the view controller itself. In many case this 
would involve using a navigation controller (UINavigationController).

To learn more about application architecture using view controllers, read the 
View Controller Programming Guide 

For general design guidelines etc. see 


To address the general question of "how do I access an object from another 
view": As you appear to be asking it, you don't.
The general pattern is that before you present a view, you pass to its view 
controller any data that will be needed for display. It's up to the view 
controller to then project the relevant information into its view.

mmalc

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Access object from another view

2010-09-21 Thread Quincey Morris
On Sep 21, 2010, at 14:03, Steve Wetzel wrote:

> I could not get the 
> 
> [self.viewController1.label2.text 
> 
> or the 
> 
> [self view addSubview:viewController2.view];
> 
> to work for some reason.  Then it dawned on me that I could set the value 
> right before I added the Subview!
> 
> [viewController2.label2 setText:label1.text];
> [self.view addSubview:viewController2.view];
> 
> This works, although for some reason the label does not update unless I 
> reload the view.  I will figure out what is going on there.

The usual cause of such behavior is the timing of NIB loading. There are 
various points at which code can be executed (init..., awakeFromNib, 
viewDidMoveToSuperview, etc), and it can be hard to keep track of the 
correctness of your assumptions of what must have happened already.

In order to transfer the label successfully, you need both view1 and view2 to 
have been loaded. Since NSViewController's 'view' method causes the view to be 
loaded if necessary, you can be certain it's done *after* executing:

[self.view addSubview:viewController2.view];

since that references both views. Did you try putting the other line:

[viewController2.label2 setText:label1.text];

*after* the 'addSubview' line instead of before?

Is the label static in view1's NIB? If so, why can't you just use IB to put the 
same label text in view2 and have no code at all?


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Access object from another view

2010-09-21 Thread Steve Wetzel
I think that is what I did with the line 

[viewController2.label2 setText:label1.text];

Then [self.view addSubView:viewController2.view]; brings up the subview.

The only problem is that the text in label two is not displayed correctly until 
I release and reload the view.  I have a button to remove view which fires the 
code [self.view removefromSuperview];  then when i bring the view back up with 
the above code, the label has the correct text in it.

This seems to be something pretty simple and yet I am struggling with it.  What 
is a good resource for me to read that can give me a basic understanding of 
things like this?  Why do I have to bring this view up twice in order to see 
the proper text in the label?

Steve






On Sep 21, 2010, at Sep 21:4:11 PM, Eric E. Dolecki wrote:

> You could make a public method in the second and call that from the first to 
> set the text too I suppose.
> 
> 
>   Google Voice: (508) 656-0622
>   Twitter: eric_dolecki  XBoxLive: edolecki  PSN: eric_dolecki
>   http://blog.ericd.net
> 
> 
> 
> On Tue, Sep 21, 2010 at 5:03 PM, Steve Wetzel  wrote:
> Thanks Everyone.
> 
> I could not get the
> 
> [self.viewController1.label2.text
> 
> or the
> 
> [self view addSubview:viewController2.view];
> 
> to work for some reason.  Then it dawned on me that I could set the value 
> right before I added the Subview!
> 
> [viewController2.label2 setText:label1.text];
> [self.view addSubview:viewController2.view];
> 
> This works, although for some reason the label does not update unless I 
> reload the view.  I will figure out what is going on there.
> 
> Thanks for the suggestions.
> 
> Steve
> 
> 
> 
> 
> 
> 
> On Sep 21, 2010, at Sep 21:3:25 PM, Conrad Shultz wrote:
> 
> > -BEGIN PGP SIGNED MESSAGE-
> > Hash: SHA1
> >
> > On 9/21/10 11:57 AM, Steve Wetzel wrote:
> >> How do I access and object on one view from another view?  I cannot
> >> figure it out.
> >>
> >> I have two view controllers and two views.  Lets call them
> >> viewController1 and viewController2 and view1 and view2.
> >> ViewController1 loads the second view by:
> >>
> >> [self.view addSubview:viewController2.view];
> >>
> >> There is a label in view1 that I want to get the value of the text
> >> from in the code for viewController2.  How do I do this?
> >
> > Caveat: you ought to listen to Quincey's advice, since it will give a
> > more robust, or at least a cleaner, implementation.
> >
> > That said, for the specific situation at hand, UIView has a superview
> > property that returns a reference to, well, the view's superview (if it
> > exists).  If you have the label as a property of view1 ("theLabel"), you
> > could do something like the following (in viewController2):
> >
> > UILabel *theLabel = [[[self view] superview] theLabel];
> >
> > If it's a tagged view, you could do something like:
> >
> > NSInteger labelTag = (whatever you set, probably in IB);
> > UILabel *theLabel = [[[self view] superview] viewWithTag:labelTag];
> >
> > (Since properties are involved, you are of course free to use dot syntax
> > in many places above if that's your preference.)
> >
> > - --
> > Conrad Shultz
> >
> > Synthetiq Solutions
> > www.synthetiqsolutions.com
> > -BEGIN PGP SIGNATURE-
> > Version: GnuPG v1.4.7 (Darwin)
> > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> >
> > iD8DBQFMmRSvaOlrz5+0JdURApMYAJ0aAv996lOHMDyY5oTKg0ju0LIbIACePwij
> > i3EBvDqS2WGzo+wnrBT9Mz8=
> > =Ea2B
> > -END PGP SIGNATURE-
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/edolecki%40gmail.com
> 
> This email sent to edole...@gmail.com
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Access object from another view

2010-09-21 Thread Steve Wetzel
Thanks Everyone.

I could not get the 

[self.viewController1.label2.text 

or the 

[self view addSubview:viewController2.view];

to work for some reason.  Then it dawned on me that I could set the value right 
before I added the Subview!

[viewController2.label2 setText:label1.text];
[self.view addSubview:viewController2.view];

This works, although for some reason the label does not update unless I reload 
the view.  I will figure out what is going on there.

Thanks for the suggestions.

Steve






On Sep 21, 2010, at Sep 21:3:25 PM, Conrad Shultz wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
> 
> On 9/21/10 11:57 AM, Steve Wetzel wrote:
>> How do I access and object on one view from another view?  I cannot
>> figure it out.
>> 
>> I have two view controllers and two views.  Lets call them
>> viewController1 and viewController2 and view1 and view2.
>> ViewController1 loads the second view by:
>> 
>> [self.view addSubview:viewController2.view];
>> 
>> There is a label in view1 that I want to get the value of the text
>> from in the code for viewController2.  How do I do this?
> 
> Caveat: you ought to listen to Quincey's advice, since it will give a
> more robust, or at least a cleaner, implementation.
> 
> That said, for the specific situation at hand, UIView has a superview
> property that returns a reference to, well, the view's superview (if it
> exists).  If you have the label as a property of view1 ("theLabel"), you
> could do something like the following (in viewController2):
> 
> UILabel *theLabel = [[[self view] superview] theLabel];
> 
> If it's a tagged view, you could do something like:
> 
> NSInteger labelTag = (whatever you set, probably in IB);
> UILabel *theLabel = [[[self view] superview] viewWithTag:labelTag];
> 
> (Since properties are involved, you are of course free to use dot syntax
> in many places above if that's your preference.)
> 
> - -- 
> Conrad Shultz
> 
> Synthetiq Solutions
> www.synthetiqsolutions.com
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.7 (Darwin)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iD8DBQFMmRSvaOlrz5+0JdURApMYAJ0aAv996lOHMDyY5oTKg0ju0LIbIACePwij
> i3EBvDqS2WGzo+wnrBT9Mz8=
> =Ea2B
> -END PGP SIGNATURE-

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Access object from another view

2010-09-21 Thread Conrad Shultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 9/21/10 11:57 AM, Steve Wetzel wrote:
> How do I access and object on one view from another view?  I cannot
> figure it out.
> 
> I have two view controllers and two views.  Lets call them
> viewController1 and viewController2 and view1 and view2.
> ViewController1 loads the second view by:
> 
> [self.view addSubview:viewController2.view];
> 
> There is a label in view1 that I want to get the value of the text
> from in the code for viewController2.  How do I do this?

Caveat: you ought to listen to Quincey's advice, since it will give a
more robust, or at least a cleaner, implementation.

That said, for the specific situation at hand, UIView has a superview
property that returns a reference to, well, the view's superview (if it
exists).  If you have the label as a property of view1 ("theLabel"), you
could do something like the following (in viewController2):

UILabel *theLabel = [[[self view] superview] theLabel];

If it's a tagged view, you could do something like:

NSInteger labelTag = (whatever you set, probably in IB);
UILabel *theLabel = [[[self view] superview] viewWithTag:labelTag];

(Since properties are involved, you are of course free to use dot syntax
in many places above if that's your preference.)

- -- 
Conrad Shultz

Synthetiq Solutions
www.synthetiqsolutions.com
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFMmRSvaOlrz5+0JdURApMYAJ0aAv996lOHMDyY5oTKg0ju0LIbIACePwij
i3EBvDqS2WGzo+wnrBT9Mz8=
=Ea2B
-END PGP SIGNATURE-
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Access object from another view

2010-09-21 Thread Steve Wetzel
I hope I am replying correctly, I am new at this.

The text in label1 is set with the setText method when the view is loaded.

What I am trying to achieve is putting some text whose value is in a label on 
view1 into a label on view2 (called label2).  I was hoping to do with with 
something likeā€¦

[label2 setText:;

of course this code would be going in viewController2 .

The objects are all created in NIB files.  The labels do not need to be 
dynamically linked. I just need this done when the view is loaded with the 
addSubview method.

As for which views and controllers are aware of or have references to other 
views and controller, well that is the issue.  I don't know how to reference an 
object on a view outside of the view controller I am working in.  Surely there 
must be a way to do this.

Steve



On Sep 21, 2010, at Sep 21:2:27 PM, Quincey Morris wrote:

> On Sep 21, 2010, at 11:57, Steve Wetzel wrote:
> 
>> How do I access and object on one view from another view?  I cannot figure 
>> it out.
>> 
>> I have two view controllers and two views.  Lets call them viewController1 
>> and viewController2 and view1 and view2.  ViewController1 loads the second 
>> view by:
>> 
>> [self.view addSubview:viewController2.view];
>> 
>> There is a label in view1 that I want to get the value of the text from in 
>> the code for viewController2.  How do I do this?
> 
> Probably no one can advise you on the *best* way to do this without knowing 
> exactly what you're trying to achieve. (Are there NIB files involved? How is 
> the label initialized in view1? Does the label change during execution in 
> such a way that view2 must be dynamically updated to match view1? Which views 
> and controllers are aware of -- have references to -- the other views and 
> controllers?)
> 
> The answer probably involves making the text value of the label a property of 
> viewController1, and propagating it to the other places via bindings, KVO or 
> setting via outlets.
> 
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Access object from another view

2010-09-21 Thread Quincey Morris
On Sep 21, 2010, at 11:57, Steve Wetzel wrote:

> How do I access and object on one view from another view?  I cannot figure it 
> out.
> 
> I have two view controllers and two views.  Lets call them viewController1 
> and viewController2 and view1 and view2.  ViewController1 loads the second 
> view by:
> 
> [self.view addSubview:viewController2.view];
> 
> There is a label in view1 that I want to get the value of the text from in 
> the code for viewController2.  How do I do this?

Probably no one can advise you on the *best* way to do this without knowing 
exactly what you're trying to achieve. (Are there NIB files involved? How is 
the label initialized in view1? Does the label change during execution in such 
a way that view2 must be dynamically updated to match view1? Which views and 
controllers are aware of -- have references to -- the other views and 
controllers?)

The answer probably involves making the text value of the label a property of 
viewController1, and propagating it to the other places via bindings, KVO or 
setting via outlets.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com