我广泛搜索了委托教程,但无法使其正常工作。这是故事和代码。DetailViewController 有一个 UITextField 和一个 UIButton。当您按下按钮时,您将进入另一个带有简单分段表的 PricelistViewController。点击该表中的一行应将行标题中的文本返回到第一个视图并将其插入到文本字段中。但事实并非如此。这是代码:
PricelistViewController.h(第二个视图):
@class PricelistViewController;
@protocol PricelistViewControllerDelegate <NSObject>
@required
//- (void)theSaveButtonOnThePriceListWasTapped:(PricelistViewController *)controller didUpdateValue:(NSString *)value;
- (void)theSaveButtonOnThePriceListWasTapped:(NSString *)value;
@end
@interface PricelistViewController : UITableViewController
@property (nonatomic, weak) id <PricelistViewControllerDelegate> delegate;
@property (retain, nonatomic) NSMutableArray * listOfSections;
@end
PricelistViewController.m
@synthesize delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [listOfSections objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"ProductSections"];
NSString *selectedProduct = [array objectAtIndex:indexPath.row];
[self.delegate theSaveButtonOnThePriceListWasTapped:[NSString stringWithFormat:@"%@", selectedProduct]];
[self.navigationController popViewControllerAnimated:YES];
}
这是DetailViewController.h中的代码(第一个带有文本字段和按钮的视图):
#import "PricelistViewController.h"
@interface DetailViewController : UIViewController <UITextFieldDelegate, PricelistViewControllerDelegate>
DetailViewController.m(这是我尝试更改字段中文本的方式):
- (void)theSaveButtonOnThePriceListWasTapped:(NSString *)value
{
NSLog(@"Text, sent here: %@", value);
NSLog(@"Text was sent here.");
self.detailDescriptionLabel.text = value;
}
detailDescriptionLabel是要显示的文本的 UITextField。
有人可以检查代码并提供帮助吗?我在这件事上工作了两天,没有运气!