Search code examples
iosobjective-coopios6delegates

trying to learn objective-c delegates. why wont my sample project work?


I am trying to pass data between two view controllers. viewcontroller2 is a delegate of viewcontroller.. note.. i called the delegate property "homie" yes I know this is bad practice but I am just messing around trying to understand the concept.

here is viewcontroller:

#import <UIKit/UIKit.h>
@protocol ViewControllerDelegate <NSObject>
@required
- (void)sendData:(NSString *)theString;
@end

@interface ViewController : UIViewController
@property (nonatomic, assign) id <ViewControllerDelegate> homie;
- (void)doSomething;
- (IBAction)doneText:(id)sender;

@end

implementation:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)doSomething{
}

- (IBAction)doneText:(id)sender {
    UITextField *thisField = sender;

    if([_homie respondsToSelector:@selector(sendData:)]){
        [_homie sendData:[thisField text]];
    }
}

now here is the other view controller implementing the first

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

@interface ViewController2 : UIViewController <ViewControllerDelegate>

- (IBAction)hittingbtn:(id)sender;

@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (strong, nonatomic) ViewController *vc;

@end

implementation:

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    ViewController *theview = [[ViewController alloc]init];
    theview.homie = self;

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// DELEGATE METHOD
- (void)sendData:(NSString *)theString{
    [_textfield setText:theString];
}

- (IBAction)hittingbtn:(id)sender {
}
@end

in viewdidload I instantiate the first viewcontroller, and set myself as its delegate. im assuming then when that viewcontroller2 runs the code in its method then it will see if its delegate has implemented the delegate method and use that code.. what am i missing here?


Solution

  • Your problem is that you use ViewControllers wrong. You have initialized ViewController

    ViewController *theview = [[ViewController alloc]init];
    theview.homie = self;
    

    And what next? You haven't assign it to your properties or something. What have you do? It's simple. You have to implement - (void)prepareForSegue:sender:. First of all in your storyboard set segue's identifier. Then do following.

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"MySegue"]) {
            ViewController *destinationViewController = (ViewController *)[segue destinationViewController];
            ViewController.homie = self;
        }
    }