2015-09-11 31 views
2

我正在學習目標c並需要幫助來了解委託。與委託不起作用的目標c文本字段

我在FirstViewController中有一個UITextField,我想在SecondViewController中顯示inputed文本。

但它不工作。我究竟做錯了什麼?

這裏是我的代碼:

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 

而且SecondViewContro ller.m

#import "SecondViewController.h" 

@interface SecondViewController() 

@end 

@implementation SecondViewController 

@synthesize candidateLabel; 
@synthesize myDelegate; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

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

@end 
+0

做一些調試。是否調用了saveCodeString方法? 'getCodeString'設置爲期望值嗎? 'self.candidateLabel'設置爲非零值嗎? – rmaddy

回答

-1

您是否在故事板中設置了UITextfield的代理?

或嘗試

@interface FirstViewController組代表:UIViewController中<UITextfieldDelegate>

如果仍然存在問題。請提供鏈接到源代碼,我喜歡看看它。

+0

1)您可以看到在OP代碼中設置的委託。2)將該協議添加到「@ interface」行僅用於編譯。這與設置委託沒有任何關係。 – rmaddy

0

我相信你正在以錯誤的方式對SecondViewController執行segue。你有沒有從FirstViewControllerSecondViewController工作?

試試這個:

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

嗨。謝謝回答。從FirstView到SecondView的細節工作正常。我已經嘗試了你的解決方案,但是我仍然看不到在SecondViewController中的候選標籤中的FirstViewController中輸入的文本。 –

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 

享受!

0

我看見你找到了另一種方式,但無論如何,你不得不改變getCodeStringstrong

#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 
+0

你不使用'strong'和'retain'。強「是ARC,」保留「是MRC。 – rmaddy