3

編集 1:問題を把握したと思いますが、解決できていません。textFieldShouldReturn が定義されている SecondViewController.m では、NSLog がトリガーされなかったため、"if ([self.delegate RespondsToSelector:@selector(passString:)])" は false を返します。

編集 2:両方の ViewController で誰がデリゲートであるかの NSLogs を確認すると、問題に対する洞察が得られました。FirstViewController では、prepareForSegue が正しいデリゲートを割り当てます。SecondViewController へのセグエの後、デリゲートは「null」になります。

これは、私が 1 週間達成しようとしてきたことですが、成功しませんでした。デリゲートを使用して 2 層のナビゲーション スタックにデータを戻す必要があることは理解しています。ただし、コードが機能しない理由がわかりません (文字列が渡されません)。

また、表示ビュー (第 1 コントローラー)。

これが私のコードです:

SecondViewController.h

#import <UIKit/UIKit.h>

@protocol PassStringDelegate
- (void)passString:(NSString *)stringFromTextField;
@end

@interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITableView *detailTable;

@property (weak, nonatomic) id <PassStringDelegate> delegate;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "CustomCell.h" // My cell is custom built in IB.

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize delegate;

@synthesize stringFromTextField;

- (void)viewDidLoad
{
    ...            
    NSLog(@"%@", self.delegate);                          // Returns "(null)".
}    

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Data Entry" forIndexPath:indexPath];
    [cell setEditing:YES animated:YES];
    cell.customTextField.clearsOnBeginEditing = NO;
    cell.customTextField.returnKeyType = UIReturnKeyDone;
    cell.customTextField.delegate = self;                 // customTextField is a custom textfield embedded in the custom cell.
    [self textFieldShouldReturn:cell.customTextField];    // This function supposedly will trigger passString:stringFromTextField when the cell is done with editing.

    return cell;
}

...

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(passString:)]) {
        [self.delegate passString:textField.text];
        NSLog(@"Sent back: %@", textField.text);          // I checked NSLog, this was never called.
    }
    return YES;
}

FirstViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, PassStringDelegate>

@property (strong, nonatomic) IBOutlet UITableView *table;

@property (copy, nonatomic) NSArray *myList;

@property (weak, nonatomic) NSString *obtainedString;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "CustomCell.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize obtainedString;

...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self passString:obtainedString];
}

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Display" forIndexPath:indexPath];
    cell.customLabel.text = obtainedString;             // I want the label to display the text in the text field in the second controller.
    cell.detailCustomLabel.text = self.myList[indexPath.row];

    return cell;
}

...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segue"])
     {
        SecondViewController *detailedView = [[SecondViewController alloc] init];
        detailedView.delegate = self;
        NSLog(@"%@", detailedView.delegate);            // Returns "<SecondViewController: 0x10967b590>".
     }
}

...

- (void)passString:(NSString *)stringFromTextField
{
    obtainedString = stringFromTextField;
}

私のデリゲート パスと呼び出しをチェックして、私が行ったことが適切に近いかどうかを確認してください。どうもありがとうございました!

4

2 に答える 2

2

これを試して:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segue"]) {
        //SecondViewController *detailedView = [[SecondViewController alloc] init];
        //this creates a new object of SecondViewController that has nothing to do
        //with the segue

        //this should fix it:
        SecondViewController *detailedView = [segue destinationViewController];
        detailedView.delegate = self;
    }
}

の新しいオブジェクトを作成しSecondViewController、デリゲートを設定していました。悲しいことに、セグエはすでにオブジェクトを作成しており、ここでインスタンス化したオブジェクトの代わりにその
オブジェクト をプッシュしているため、このオブジェクトはまったく使用されていませんでした。

于 2014-08-31T15:49:49.827 に答える