2
votes

I'm learning objective c and need help to understand delegate.

I have a UITextField in the FirstViewController and I want to show the text inputed in the SecondViewController.

But it's not working. What am I doing wrong?

Here is my code:

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIButton *accessButton;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (nonatomic, retain) NSString *getCodeString;

@end

SecondViewController.h

#import <UIKit/UIKit.h>

@class CodeField;

@protocol CodeFieldHandlerDelegate <NSObject>

@property NSString *saveCodeString;

@end

@interface SecondViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *candidateLabel;

@property (weak, nonatomic) id <CodeFieldHandlerDelegate> myDelegate;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

<CodeFieldHandlerDelegate>

@property (nonatomic, strong) SecondViewController *secondViewController;

@end

@implementation FirstViewController

@synthesize getCodeString;
@synthesize secondViewController;
@synthesize saveCodeString;

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)accessButton:(UIButton *)sender {

    //get the input data from text field and store into string
    getCodeString = self.codeField.text;

    //go keypad back when button clicked from textfield
    [self.codeField resignFirstResponder];

    secondViewController = [[SecondViewController alloc] init]; 
    secondViewController.myDelegate = self;

    [self.navigationController pushViewController:secondViewController animated:YES];

}

// name of this string is the same of the NSString in the delegate
- (NSString *) saveCodeString {
    return getCodeString;
}

@end

And SecondViewController.m

 #import "SecondViewController.h"

 @interface SecondViewController ()

 @end

 @implementation SecondViewController

 @synthesize candidateLabel;
 @synthesize myDelegate;

 - (void)viewDidLoad {
    [super viewDidLoad];

     self.candidateLabel.text = [myDelegate saveCodeString];
}

@end
4
Do some debugging. Is the saveCodeString method called? Is getCodeString set to the expect value? Is self.candidateLabel set to a non-nil value?rmaddy

4 Answers

0
votes

I believe you are performing a segue to the SecondViewController the wrong way. Have you got the segue from FirstViewController to the SecondViewController working?

Try this:

secondViewController = (SecondViewController *)[[UIStoryboard storyboardWithName:@"YOUR STORYBOARD NAME" bundle:nil] instantiateViewControllerWithIdentifier:@"YOUR CONTROLLER IDENTIFIER"];
0
votes

A friend of mine explained to me that delegate is not the best option for my purpose. So here is my final code working with some changes for the benefit of understanding:

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (strong, nonatomic) NSString *nameUser;

@end

SecondViewController.h

#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) NSString *nameUser2;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)nameField:(id)sender {

    self.nameUser = self.nameField.text;
    [self.nameField resignFirstResponder];

    // check if it works
    NSLog(@"Your name is: %@", self.nameUser);

    //in the main.storyboard create a segue 
    //from FirstViewController yellow icon to the 
    //SecondViewController and name it like: goToSecond

    [self performSegueWithIdentifier:@"goToSecond" sender:sender];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"goToSecond"]) {

        SecondViewController *vc = segue.destinationViewController;
        vc.nameUser2 = self.nameUser;
    }
}

@end

SecondViewController.m

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.nameLabel.text = self.nameUser2;
}

@end

Enjoy!

0
votes

I saw you found another way, but anyway, you had to change getCodeString to strong:

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIButton *accessButton;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (strong, nonatomic, retain) NSString *getCodeString;

@end
-1
votes

Did you set delegate of UITextfield in storyboard ??

or try set delegate in

@interface FirstViewController : UIViewController<UITextfieldDelegate>

if still problem exist. please provide link to source code ,i like to look into it.