截屏视频: https : //www.youtube.com/watch?v= ZwehDwITEyI
这真的很奇怪。问题在于标签出口位于定制设计的表格单元中。该单元格属于我的CustomCell类。(如果您观看了截屏视频,则实际上称为RA_FormCell)。
CustomCell.h
@property (weak, nonatomic) IBOutlet UILabel *dayOutlet;
-(void)controller:(id<CustomCellDelegate>)controller didUpdateDay:(NSString *)theDay;
CustomCell.m
// Method is called by a view controller
// (which is itself a delegate of the CustomCell class,
// hence the identifier you see below)
-(void)controller:(id<CustomCellDelegate>)controller didUpdateDay:(NSString *)theDay;
{
NSLog(@"Method called") // confirms to me that method is called
self.dayOutlet.text = @"Goodmorning";
NSLog(@"%@", self.dayOutlet.text); // displays (null)
}
最终的日志实际上确实出现了,因此肯定会调用该方法。我打折了以下内容:
self.dayOutlet.text
没有通过项目中的任何其他方法写入其他地方CustomCell
问题在于该controller:didUpdateDay:
消息未发送到正确的单元格类实例。
发生这种情况是因为我没有正确地将此单元分配为视图控制器的委托。对于任何有兴趣的人,在我的截屏视频(3:50)中,您都可以看到我的内容cellForRowAtIndexPath
:
RA_FormCell *cell = [tableView dequeueReusableCellWithIdentifier:self.formCellArray[indexPath.row] forIndexPath:indexPath];
self.delegate = cell
但是,这意味着self.delegate在生成表单元格时不断被覆盖。结果,我的controller:didUpdateDay
消息被发送到表的底部单元格,而不是我需要的顶部单元格。
解决方案很简单-根本不需要第二个委托。相反,当单元委托给视图控制器时,它应该传递self
给它委托的消息:
id<CustomCellDelegate> strongDelegate = self.delegate;
if ([strongDelegate respondsToSelector:@selector(customCell:didChangeDay1:)])
[strongDelegate customCell:self didChangeDay1:[sender value]];
然后,在由委托执行此方法时,只需直接更改出口即可结束该方法:
-(void)customCell:(RA_FormCell *)customCell didChangeDay1:(double)value
// put logic here
customCell.dayOutlet.text = @"No problem!";
通常,很少需要双向代理结构。保持从A
到的一种方式,B
并只记得A
传递self
它发送给的任何消息B
。这样,B
将知道消息来自的对象,并能够与之通信A
。
感谢Paulw11
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句