Osk*_*gon 4 xcode delegates objective-c ios uistoryboardsegue
我想知道是否有人可以解释在下面的示例中使用展开segue和使用委托之间的区别:
我有一个FriendsTableViewController由array朋友填充,另一个AddFriendTableViewController有一个功能添加friend到FriendsTableViewController.
这是我从我发送数据AddFriendViewController的方式unwind segue:
#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];
}
}
Run Code Online (Sandbox Code Playgroud)
这是我将数据从中添加AddFriendTableViewController到arrayin FriendsTableViewController中的方式unwind segue action:
#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];
}
}
Run Code Online (Sandbox Code Playgroud)
现在这可以按照我的意愿工作,所以我希望我不会破坏任何stackoverflow规则或冒犯任何人,但我只是想知道我的示例代码的使用方式与是否使用自定义委托制作相同的方案之间的区别的方法AddFriendViewController.如果有人可以解释它会很棒!
使用unwind segue与拥有委托非常相似,具有以下优点:
缺点是
prepareForSegue大量分支标识符名称进入你的代码看起来不错.我坚持下去.
| 归档时间: |
|
| 查看次数: |
723 次 |
| 最近记录: |