簡體   English   中英

目標c文本字段,代理不起作用

[英]Objective c text field with delegate not working

我正在學習目標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

我相信您以錯誤的方式對SecondViewController執行了segue。 您是否已從FirstViewControllerSecondViewController正常工作?

嘗試這個:

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

我的一個朋友向我解釋說,就我的目的而言,代表不是最佳選擇。 因此,這是我的最終代碼,進行了一些更改以幫助理解:

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

請享用!

我看到您找到了另一種方法,但是無論如何,您必須將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

您是否在情節提要中設置了UITextfield的委托?

或嘗試在其中設置委托

@interface FirstViewController:UIViewController <UITextfieldDelegate>

如果仍然存在問題。 請提供指向源代碼的鏈接,我喜歡研究它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM