我正在尝试将对象从表格单元格转换为 UITabBarController
。我将 segue 发送到 View Controller 的代码是这样的:
#import "DetailViewController.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"UpdateContacts"]) {
NSManagedObject *selectedDevice = [self.contactarray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
DetailViewController *destViewController = segue.destinationViewController;
destViewController.contactdb = selectedDevice;
refreshControl = nil;
}
}
我还在 DetailsViewController.h 中添加了 contactdb
属性
这个工作正常,现在我不知道如何将它转换为 UITabBarController
,所以我可以从我在 UITabBarController
中的 2 个 View Controller 访问它。
最佳答案
我不清楚,你说的DetailViewController是不是你的TabBarController?
一般来说,当你想在流程比正常情况复杂一点的 View Controller 之间传递数据时,你可以使用通知/监听器模式。例如,在传递 Controller 中你会做这样的事情:
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_REFRESH
object:contactDb];
在您的接收 Controller 中,您将像这样注册收听通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(gotData:)
name:NOTIFICATION_REFRESH
object:nil];
在同一个 Controller 中,这里是捕获通知和对象的代码:
- (void)gotData:(NSNotification *) notification {
NSObject *myData = [notification object];
}
您需要像这样在 header 中定义通知类型:
extern NSString* const NOTIFICATION_REFRESH;
它在 .m 文件中的实现是这样的:
NSString* const NOTIFICATION_REFRESH = @"notificationRefresh";
关于ios - 如何将对象转至 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21362948/