我广泛搜索了委托(delegate)教程,但无法使其正常工作。这是故事和代码。
DetailViewController 有一个 UITextField 和一个 UIButton。当您按下按钮时,您将进入另一个带有简单分段表的 PricelistViewController。点击该表中的一行应将行标题中的文本返回到第一个 View 并将其插入到文本字段中。但事实并非如此。这是代码:
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 (带有文本字段和按钮的第一个 View ):
#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;
}
详细描述标签 是要显示的文本的 UITextField。
有人可以检查代码并提供帮助吗?我在这件事上工作了两天,没有运气!
最佳答案
首先,你为什么在 PriceListViewController 的头文件中前向引用(@class)你的类?这不是必需的。
其次,您是否使用 ARC,如果您是您的数组属性应该是非原子类型,强。如果你不是你的委托(delegate)属性应该是非原子的,分配。你似乎在混淆你的术语
还有你在哪里以及如何初始化你的数组?
关于iOS 委托(delegate)不起作用,无法修复(内部代码): sending data from UITableView to UITextField in another view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971370/