Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8109901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:34:03+00:00 2026-06-06T01:34:03+00:00

I am trying to send a simple string from One viewcontroller to another viewcontroller

  • 0

I am trying to send a simple string from One viewcontroller to another viewcontroller using delegate but my delegate is not working. I am using storyboard and dont want to use segue to pass data. Here is my simple code

ViewControllerA.h

#import <UIKit/UIKit.h>

@class ViewControllerA;

@protocol viewControllerADelegate <NSObject>

-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;

@end

@interface ViewControllerA : UIViewController{
 __weak id <viewControllerADelegate> delegate;
}
- (IBAction)buttonPressed:(id)sender;

@property (weak) id<viewControllerADelegate> delegate;
@end

ViewControllerA.m

#import "ViewControllerA.h"

@interface ViewControllerA ()

@end

@implementation ViewControllerA
@synthesize delegate;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

 - (IBAction)buttonPressed:(id)sender {
[self.delegate addItem:self data:@"this is data"];

 }
 @end

ViewControllerB.h

#import <UIKit/UIKit.h>
#import "ViewControllerA.h"

@interface ViewControllerB : UIViewController<viewControllerADelegate>

@end

ViewControllerB.m

#import "ViewControllerB.h"
#import "ViewControllerA.h"

@interface ViewControllerB ()

@end

@implementation ViewControllerB

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

 - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

ViewControllerA *vca = [storyboard instantiateViewControllerWithIdentifier:@"A"];
[vca setDelegate:self];

 }

- (void)viewDidUnload
 {
[super viewDidUnload];
// Release any retained subviews of the main view.
 }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item{
NSLog(@"delegate function called");
 }

 @end

Am I missing something silly ? The delegate method -(void) addItem: (ViewControllerA *)controller data:(NSString *)item is not triggered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:34:05+00:00Added an answer on June 6, 2026 at 1:34 am

    The flaw in your design is to try sending data with delegates to an object you create yourself. You certainly don’t need delegates for that as you can do it with simple properties. myViewControllerBObject.dataProperty = @"some data"]; is simply enough. What delegation is usually used for is send data back to the controller that created the current object or to any other controller somewhere in your app.

    For example, move the delegate code in ViewControllerA to ViewControllerB and add a UIButton and an IBAction to ViewControllerB that will send the data back to ViewControllerA. Here it goes:

    ViewControllerB.h

    #import <UIKit/UIKit.h>
    
    @class ViewControllerB;
    
    @protocol viewControllerBDelegate <NSObject>
    
    -(void) sendDataBack: (ViewControllerB *)controller data:(NSString *)item;
    
    @end
    
    @interface ViewControllerB : UIViewController
    {
       __unsafe_unretained id <viewControllerBDelegate> delegate;
    }
    
    @property (nonatomic, assign) id<viewControllerBDelegate> delegate;
    - (IBAction)buttonTouch:(id)sender;
    
    @end
    

    ViewControllerB.m

    #import "ViewControllerB.h"
    
    @interface ViewControllerB ()
    
    @end
    
    @implementation ViewControllerB
    @synthesize delegate;
    ....
    - (IBAction)buttonTouch:(id)sender {
        [self.delegate sendDataBack:self data:@"my data"];
    }
    @end
    

    And in ViewControllerA, you first need to get a hold of that pushed ViewControllerB object via segues. Then implement the delegate method.

    ViewControllerA.h

    #import <UIKit/UIKit.h>
    #import "ViewControllerB.h"
    
    @interface ViewControllerA : UIViewController <viewControllerBDelegate>
    - (IBAction)buttonPressed:(id)sender;
    
    @end
    

    ViewControllerA.m

    #import "ViewControllerA.h"
    
    @interface ViewControllerA ()
    
    @end
    
    @implementation ViewControllerA
    ......
    - (IBAction)buttonPressed:(id)sender {
    
    }
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        NSLog(@"id %@", [segue destinationViewController]);
        ViewControllerB *vcb = (ViewControllerB *)[segue destinationViewController];
        vcb.delegate = self;  //key point
    }
    - (void)sendDataBack:(ViewControllerB *)controller data:(NSString *)item
    {
        NSLog(@"Here is the data %@ and the controller it comes from %@", item, controller);
    }
    @end
    

    Hope this will help you get a better understanding of communication with delegates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send simple string message from my desktop app using sockets
I am trying to send audio file from one computer to other using socket
I'm trying to send pdf attachments in one of my rails applications, but the
I am trying to write code to send a simple mail from asp.net page.
I am trying to send sms from java application using SMPPSim as SMS gateway
I'm trying to create a very simple database abstraction, one part of it using
I am trying to get send json from my javascript (using jquery post) to
I've been trying to send a string to/from C# to/from C++ for a long
HI, I am trying to send a simple notification using system.net.mail.mailmessage. I just pass
im attempting to send a string from one of my activities to the main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.