【发布时间】:2014-05-13 14:07:21
【问题描述】:
我想知道是否有人可以在下面的示例中解释使用 unwind segue 和使用委托之间的区别:
我有一个FriendsTableViewController 由朋友的array 填充,另一个AddFriendTableViewController 具有将friend 添加到FriendsTableViewController 的功能。
这就是我使用unwind segue 从AddFriendViewController 发送数据的方式:
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Check whether the Done button was tapped.
// If it wasn’t, instead of saving the friend, the method returns without doing anything else.
if (sender != self.doneButton) return;
// See whether there’s text in the text field.
if (self.nameTextField.text.length > 0) {
// If there’s text, create a new friend and give it's properties the input from the text fields.
self.friend = [[Friend alloc] initWithName:self.nameTextField.text
dateOfBirth:self.birthdayDatePicker.date
numberOfGifts:0];
}
}
这就是我使用unwind segue action 将AddFriendTableViewController 中的数据添加到FriendsTableViewController 中的array 的方法:
#pragma mark - Actions
- (IBAction)unwindSegue:(UIStoryboardSegue *)segue
{
// Retrieve the source view controller, the controller that is unwinding from.
AddFriendTableViewController *soruce = [segue sourceViewController];
// Retrieve the soruce view controller’s friend object.
Friend *friend = soruce.friend;
// See whether the item exists.
// If it’s nil, either the Cancel button closed the screen or the text field had no text, so you don’t want to save the item.
if (friend != nil) {
// If it does exist, add the item to the friends array.
[self.friends addObject:friend];
// Reload the data in the table.
[self.tableView reloadData];
}
}
现在这可以按我的意愿工作,所以我希望我没有违反任何 stackoverflow 规则或冒犯任何人,但我只是想知道我的示例代码的使用方式与是否进行了相同的场景之间有什么区别,但是使用AddFriendViewController 的自定义委托方法。如果有人能解释就太好了!
【问题讨论】:
标签: ios objective-c xcode delegates uistoryboardsegue