0

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

Community
  • 1
  • 1
Arpit Goel
  • 163
  • 1
  • 16

1 Answers1

3

You can achieve this by using Delegation in Objective-C.

In your SecondView.h add following right after Header Inclusion

@protocol YourDelegateName <NSObject>
   -(void)setText:(NSString *)strData;
@end

Also add delegate property to your header for accessing them in calling class, like below (This goes with other properties declaration in SecondView.h file):

@property (nonatomic, weak) id<YourDelegateName> delegate;

Now, Comes the calling the delegate part. Say, you want to save the text value of UITextView of SeconView in strTextViewData of FirstView class, when the following event occurs:

- (IBAction)save:(id)sender
{
    [self.delegate setText:self.txtView.text]; // Assuming txtView is name for UITextView object
}

Now, In FirstView.h add YourDelegateName in delegate list like below:

@interface FisrtView : ViewController <YourDelegateName>
    @property (nonatomic, reatin) NSString *strTextViewData;
@end

And then in FisrtView.m file when you create instance of SecondView class, set delegate to self like below:

SecondView *obj = [[SecondView alloc] initWithNibName:@"SeconView" bundle:nil];
obj.delegate = self; // THIS IS THE IMPORTANT PART. DON'T MISS THIS.

Now, Implement the delegate method:

-(void)setText:(NSString *)strData
{
     self.strTextViewData = strData;
}

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.

Anon
  • 623
  • 3
  • 10
  • 1
    Some explanation about Delegate would be great within the answer, as this answer cover the topic with minimal code. – viral Dec 12 '14 at 21:48
  • @richa Thanks a lot. That explanation is really helpful, and delegates is the right thing to learn for me here.. When I executed the code you gave, I get the following error.. `Thread 1:EXC_BAD_ACCESS(code=2, address=0x27c7ff4)` I absolutely don't understand what this means. Am I missing something obvious here ? Thanks! – Arpit Goel Dec 12 '14 at 23:59
  • The only difference in my code and what you gave is that I am instantiating the SecondView using Storyboard as follows.. `UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];` `SecondView *sView = [story instantiateViewControllerWithIdentifier:@"SecondView"]; ` – Arpit Goel Dec 13 '14 at 00:02
  • I am basically running into an infinite loop.. trying to understand what's wrong ... – Arpit Goel Dec 13 '14 at 00:39
  • @user3580099 as I have mentioned there. Setting the delegate to self is an important part. Are you sure you have set the delegate to self, properly? – Anon Dec 13 '14 at 10:03
  • Hi Richa, Thanks a lot. I actually found my mistake. I was using NSString *text as a variable, and using setText which was getting confused with the setter of the variable. Really bad mistake on my part. Your code was really helpful! Thanks a lot! And delegates are brilliant1 – Arpit Goel Dec 13 '14 at 20:24