Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling A Method With A Delegate in Objective-C (iOS)
    primarykey
    data
    text
    <p>I tried to replicade a couple of posts here on stackoverflow, for passing data and make calls with 2 view controllers, but without success.</p> <p>So I have my <strong>ViewController (1st)</strong> and <strong>EditableViewController (2nd)</strong></p> <p>When I'm in my <strong>EditableViewController (2nd)</strong>, I click a Button that triggers an IBAction with the method <strong>SendTextToViewController</strong>. After that, I was expecting for the method <strong>didReceiveMessage</strong>, in my <strong>ViewController (1st)</strong> to run, and show me "BANANAS" with NSLog.</p> <p>Ultimately, <strong>I want to send data from my second Controller, to the First Controller</strong>.</p> <p>I'm having a hard time understanding this delegates thing. And I have read a dozen tutorials, videos e documentation trying to figure this out. I think I'm almost there, but can't seem to figure why this is not working.</p> <p>Can anyone help me please? The code is bellow.</p> <p>Thank you!</p> <p><strong>// ViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @protocol ViewControllerDelegate &lt;NSObject&gt; -(void)didReceiveMessage:(NSString *)string; @end @interface ViewController : UIViewController &lt;ViewControllerDelegate&gt;; @end </code></pre> <p><strong>// ViewController.m</strong></p> <pre><code>#import "ViewController.h" @implementation ViewController -(void)didReceiveMessage:(NSString *)string{ NSLog(@"BANANAS"); } @end </code></pre> <p><strong>// EditableViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @protocol ViewControllerDelegate; @interface EditableViewController : UIViewController @property (nonatomic, weak) id&lt;ViewControllerDelegate&gt; delegate; @property (nonatomic, weak) IBOutlet UITextField *TextField; @property (nonatomic, weak) IBOutlet UIButton *SendTextToViewController; - (IBAction)SendTextToViewController:(id)sender; @end </code></pre> <p><strong>// EditableViewController.m</strong></p> <pre><code>#import "EditableViewController.h" #import "ViewController.h" @implementation EditableViewController @synthesize delegate; @synthesize TextField; @synthesize SendTextToViewController; - (IBAction)SendTextToViewController:(id)sender { [delegate didReceiveMessage:TextField.text]; } - (void)viewDidUnload { [self setTextField:nil]; [super viewDidUnload]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload