ios - 未调用自定义委托(delegate)方法
<p><p>我正在尝试将 Apple 的 TableViewUpdates 示例(扩展 TableView 单元格)适应我自己的应用程序,但我似乎无法让它工作。</p>
<p>我试着缩小问题的范围,我想我现在知道问题出在哪里了。</p>
<p>Apple 使用 UITableViewController 作为 View 的基本 Controller ,但我有一个 UIViewController,它具有 UITableViewDelegate 和 DataSource 作为委托(delegate)方法。我像这样添加了 HeaderViewDelegate:</p>
<pre><code>@interface SearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, IngredientHeaderViewDelegate>
</code></pre>
<p>IngredientHeaderFooterView.h:</p>
<pre><code>#import <Foundation/Foundation.h>
#include <UIKit/UIKit.h>
@protocol IngredientHeaderViewDelegate;
@interface IngredientHeaderFooterView : UITableViewHeaderFooterView
@property (nonatomic, weak) IBOutlet UILabel *lblTitle;
@property (nonatomic, weak) IBOutlet id <IngredientHeaderViewDelegate> delegate;
@property (nonatomic) NSInteger section;
- (void)toggleOpenWithUserAction:(BOOL)userAction;
@end
@protocol IngredientHeaderViewDelegate <NSObject>
@property (nonatomic) NSInteger hoi;
@optional
- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionOpened:(NSInteger)section;
- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionClosed:(NSInteger)section;
@end
</code></pre>
<p>在 IngredientHeaderFooterView.m 中:</p>
<pre><code>- (void)toggleOpenWithUserAction:(BOOL)userAction {
if () {
NSLog(@"Test1");
;
}
if () {
NSLog(@"Test2");
;
}
}
</code></pre>
<p>在我实现委托(delegate)的 UIViewController 中:</p>
<pre><code>- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
IngredientHeaderFooterView *ingredientHeaderView = ;
IngredientDescriptionInfo *ingredientInfo = (self.sectionInfoArray);
ingredientInfo.headerView = ingredientHeaderView;
ingredientHeaderView.lblTitle.text = ingredientInfo.play.name;
ingredientHeaderView.section = section;
ingredientHeaderView.delegate = self;
return ingredientHeaderView;
}
</code></pre>
<p>但 respondsToSelector: 总是返回 false。会是什么?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在您的 <code>SearchViewController</code> 中,您需要从协议(protocol) <code>IngredientHeaderViewDelegate</code> 实现这两种方法:</p>
<pre><code>- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionOpened:(NSInteger)section
{
NSLog(@"section opened");
}
- (void)sectionHeaderView:(IngredientHeaderFooterView *)sectionHeaderView sectionClosed:(NSInteger)section
{
NSLog(@"section closed");
}
</code></pre>
<p>另外,不要忘记在 <code>IngredientHeaderFooterView</code> 中实际分配委托(delegate)。确保调用 <code>toggleOpenWithUserAction:</code> 时它不是 <code>nil</code>。</p>
<p>如果您确保方法已实现并且 <code>delegate</code> 已实际分配,那么您应该很好:)</p></p>
<p style="font-size: 20px;">关于ios - 未调用自定义委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27485309/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27485309/
</a>
</p>
页:
[1]