目标c文本字段,代表不工作

时间:2015-09-11 00:17:05

标签: ios objective-c cocoa-touch delegates

我正在学习目标c并需要帮助来理解代表。

我在FirstViewController中有一个UITextField,我想在SecondViewController中显示输入的文本。

但它没有用。我做错了什么?

这是我的代码:

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

和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 个答案:

答案 0 :(得分:0)

我相信你正在以错误的方式对SecondViewController进行调整。你有从FirstViewControllerSecondViewController工作的segue吗?

试试这个:

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

答案 1 :(得分:0)

我的一位朋友向我解释说,代表不是我的最佳选择。所以这是我的最终代码,为了理解的利益而进行了一些更改:

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

享受!

答案 2 :(得分:0)

我看到你发现了另一种方式,但无论如何,你必须将getCodeString更改为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

答案 3 :(得分:-1)

你是否在故事板中设置了UITextfield的代表?

或尝试在

中设置委托

@interface FirstViewController:UIViewController <UITextfieldDelegate>

如果仍存在问题。请提供源代码链接,我想查看它。