I am trying to do the following, and not able to find a straightforward answer.. It is related to this :Passing uitextfield from one view to another. But not exactly.
I have a Firstview.m, from which I push to a Secondview.m. The Secondview.m has a UITextView. I allow the user to edit the UITextView on Secondview.m. Now I want to store this text value in a variable in Firstview.m. One way to to do this is as follows
in Firstview.h
@property (nonatomic) Secondview *secondView;
That is keep a secondView variable in Firstview itself. But this doesn't seem efficient. Ideally I should only have 1 NSString text field in FirstView. What is the right way to do this ? Thanks
You can achieve this by using Delegation in Objective-C.
In your
SecondView.hadd following right after Header InclusionAlso add delegate property to your header for accessing them in calling class, like below (This goes with other properties declaration in
SecondView.hfile):Now, Comes the calling the delegate part. Say, you want to save the text value of
UITextViewofSeconViewinstrTextViewDataofFirstViewclass, when the following event occurs:Now, In
FirstView.haddYourDelegateNamein delegate list like below:And then in
FisrtView.mfile when you create instance ofSecondViewclass, set delegate toselflike below:Now, Implement the delegate method:
Applying this to your code will do what you want. Also, Delegation is one of the most important feature of Objective-C language, which - by doing this - you will get to learn.
Let me know, if you face any issue with this implementation.