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

Ios 代表不工作的目标c文本字段,ios,objective-c,cocoa-touch,delegates,Ios,Objective C,Cocoa Touch,Delegates,我正在学习目标c,需要帮助理解学员 我在FirstViewController中有一个UITextField,我想显示在SecondViewController中输入的文本 但它不起作用。我做错了什么 这是我的密码: FirstViewController.h #import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @property (weak, nonatomic) IBOutlet

我正在学习目标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
#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
#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
#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController

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

@end

您是否在情节提要中设置了UITextfield的委托

或者尝试设置委托

@界面FirstViewController:UIViewController


如果问题仍然存在。请提供到源代码的链接,我想查看一下。

您是否在脚本中设置了UITextfield的委托

或者尝试设置委托

@界面FirstViewController:UIViewController


如果问题仍然存在。请提供到源代码的链接,我喜欢查看它。

我相信您以错误的方式执行到
SecondViewController的步骤。从
FirstViewController
SecondViewController
的顺序是否正常

试试这个:

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

我认为您以错误的方式执行到
SecondViewController
的顺序。从
FirstViewController
SecondViewController
的顺序是否正常

试试这个:

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 UIButton *accessButton;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (nonatomic, retain) NSString *getCodeString;

@end
#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
#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
#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController

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

@end
SecondViewController.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
 #import "SecondViewController.h"

 @interface SecondViewController ()

 @end

 @implementation SecondViewController

 @synthesize candidateLabel;
 @synthesize myDelegate;

 - (void)viewDidLoad {
    [super viewDidLoad];

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

@end
#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
@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.nameLabel.text = self.nameUser2;
}

@end

享受吧

我的一位朋友向我解释说,授权并不是我的最佳选择。下面是我的最后一段代码,为了便于理解,我做了一些修改:

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
#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
#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
#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController

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

@end
SecondViewController.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
 #import "SecondViewController.h"

 @interface SecondViewController ()

 @end

 @implementation SecondViewController

 @synthesize candidateLabel;
 @synthesize myDelegate;

 - (void)viewDidLoad {
    [super viewDidLoad];

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

@end
#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
@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
#导入
@界面FirstViewController:UIViewController
@属性(弱、非原子)IBUIButton*accessButton;
@属性(弱的,非原子的)ibuitextfield*codeField;
@属性(强、非原子、保留)NSString*getCodeString;
@结束

我看到您找到了另一种方法,但无论如何,您必须将
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
#导入
@界面FirstViewController:UIViewController
@属性(弱、非原子)IBUIButton*accessButton;
@属性(弱的,非原子的)ibuitextfield*codeField;
@属性(强、非原子、保留)NSString*getCodeString;
@结束

1)您可以在OP的代码中看到正在设置的代理。2) 将协议添加到
@接口
行仅用于编译。它与设置委托没有任何关系。1)您可以在OP的代码中看到正在设置的委托。2) 将协议添加到
@接口
行仅用于编译。它与设置委托没有任何关系。请进行一些调试。是否调用了
saveCodeString
方法?
getCodeString
是否设置为期望值?
self.candidateLabel
是否设置为非零值?请执行一些调试。是否调用了
saveCodeString
方法?
getCodeString
是否设置为期望值?
self.candidateLabel
是否设置为非零值?您好。谢谢你的回答。从FirstView到SecondView的顺序工作正常。我已经尝试了您的解决方案,但仍然看不到在第二个ViewControllerHi中候选标签的FirstViewController中输入的文本。谢谢你的回答。从FirstView到SecondView的顺序工作正常。我已经尝试了您的解决方案,但仍然看不到在第二个ViewController的候选标签中的FirstViewController中输入的文本。您没有同时使用
strong
retain
strong
用于ARC,而
retain
用于MRC。您不能同时使用
strong
retain
strong
用于ARC,
retain
用于MRC。