我在故事板上有两个场景。由于我不被允许上传图片(新用户),我们称它们为场景1和场景2。
场景1:带有UILabel的UITableViewCell,当选中此单元格时,它会将您带到场景2。
场景2:为用户提供在UITableView中选择的选项。一旦选择了一个选项,它就会在选定的UITableViewCell旁边放置一个复选标记。
如何获取当您在场景2上单击保存按钮时,它会从场景2中选择的UITableViewCell中获取文本,并将用户带回场景1,并使用场景2中的文本填充UILabel?
我使用故事板来创建UITableViews。每个单元格都有自己的类。谢谢。
发布于 2013-01-01 00:31:25
使用委托设计模式允许两个对象相互通信(Apple reference)。
一般而言:
举个例子:
场景2界面
@class LabelSelectionTableViewController
@protocol LabelSelectionTableViewControllerDelegate
- (void)labelSelectionTableViewController:(LabelSelectionTableViewController *)labelSelectionTableViewController didSelectOption:(NSString *)option;
@end
@interface LabelSelectionTableViewController : UITableViewController
@property (nonatomic, strong) id <LabelSelectionTableViewControllerDelegate> delegate;
@end场景2实现
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.delegate labelSelectionTableViewController:self didSelectOption:cell.textLabel.text];
}场景1实现
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[LabelSelectionTableViewController class]] == YES)
{
((LabelSelectionTableViewController *)segue.destinationViewController).delegate = self;
}
}
// a selection was made in scene 2
- (void)labelSelectionTableViewController:(LabelSelectionTableViewController *)labelSelectionTableViewController didSelectOption:(NSString *)option
{
// update the model based on the option selected, if any
[self dismissViewControllerAnimated:YES completion:nil];
}https://stackoverflow.com/questions/14103720
复制相似问题