3

编辑1:我想我已经找到了问题,我只是无法解决它。在定义 textFieldShouldReturn 的 SecondViewController.m 中,“if ([self.delegate respondsToSelector:@selector(passString:)])”返回 false,因为没有触发 NSLog。

编辑 2:检查谁是两个 ViewController 中的委托人的 NSLogs 可以很好地了解问题。在 FirstViewController 中,prepareForSegue 分配了正确的委托。在转到 SecondViewController 之后,委托变为“null”。

这是我一周来一直试图实现的目标,但没有成功。我明白我需要使用委托将数据传回两层导航堆栈。但是,我不确定为什么我的代码不起作用(字符串没有通过)。

如果有人能告诉我(链接和其他资源)如何正确地制作表单/提交视图(第二个控制器)以用于数据输入,可以重复用于编辑显示视图(第一个控制器)中的特定单元格标签,我将不胜感激)。

这是我的代码:

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

第二视图控制器.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;
}

第一视图控制器.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

第一视图控制器.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并在其上设置一个委托。
遗憾的是,这个对象根本没有被使用,因为 segue 已经创建了一个对象并推送对象而不是您在此处实例化的对象。

于 2014-08-31T15:49:49.827 回答
0

在第二个视图控制器中以及在您推送 SecondViewControllor 之前添加一个属性 @property(保留)FirstViewControllor *parentController

添加

secondviewcontrollor.parentController=self;

并且在

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    (FirstViewControllor)self.parentController.obtainedString=textField.text;
}
于 2014-08-31T09:44:21.913 回答