我有UICollectionViewController和自定义UICollectionViewCell,其中包含子UICollectionView和自定义childUICollectionViewCell。
- UICollectionViewCell(childCell)
- UILable (valueLable)
- UIButton (Submit)
我想发送文本的"valueLable“点击”提交“按钮到新的UIViewController。请帮帮我..。
发布于 2015-02-06 11:39:55
使用方法SubmissionDelegateProtocol
创建协议-(void) submitWithValue:(NSString*)text;
您的UICollectionViewController实现(符合)协议。它应该为childViews提供这个方法来调用它。
在创建集合视图和进一步的集合及其集合视图时,您必须传递一个--比方说- sumbmitDelegate属性(或init参数),该属性包含对实现协议的UICollectionViewController的引用。
id <SubmissionDelegateProtocol> submissionDelegate;
当您使用情节提要时,prepareForSegue:
方法是将更多信息传递给后续视图控制器的合适位置。但是,如果两个集合视图都使用与它们的ViewContoller相同的dataSource (正如树所建议的那样),那么它应该更容易一些。
这样,最终每个单元格都保持对视图控制器的(弱)引用。然后,在按下按钮操作时,调用委托方法[submissionDelegate submitWithValue:valueLable ];
这是代表的模式。
或者,您可以使用通知。
发布于 2015-02-06 11:54:23
试试NSNotification Center
在子视图控制器中,postNotifictaion
在UIViewController
中,请收听相同的通知。
您还可以使用NSNotificationCenter
传递数据。
- (void) submitButtonClicked
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"dataFromChildToParent" object:nil userInfo: valueLable.text];
}
在UIViewController
的ViewDidLoad方法中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadRegistrationNotification:) name:@"dataFromChildToParent" object:nil];
并实现选择器。
- (void) loadRegistrationNotification:(NSNotification *)noti {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"dataFromChildToParent" object:nil];
NSString *valueLabelText = [noti userInfo];
NSLog(@"Notification data %@", valueLabelText);
}
有关NSNotificationCenter
的详细说明,请参阅此教程
https://stackoverflow.com/questions/28364672
复制相似问题