Ios 如何使用SWRevealViewController在两个视图之间传递数组?
快速背景: 我有一个应用程序,可以在可搜索的选项卡式界面(又称“Cheers View Controller”)中显示歌曲列表。这里有两个标签,每个标签对应我的欢呼声。我有一个侧边栏,可以让你选择菜单选项,其中一个叫做“我的收藏夹”。它使用SWREVEL控制器(也称为“侧栏”)。我试图让用户在比赛前标记最喜欢的欢呼声,然后在休息区使用侧栏导航快速参考他们最喜欢的歌曲列表 我的应用程序的初始视图控制器是显示视图控制器Ios 如何使用SWRevealViewController在两个视图之间传递数组?,ios,iphone,objective-c,cocoa-touch,xcode5,Ios,Iphone,Objective C,Cocoa Touch,Xcode5,快速背景: 我有一个应用程序,可以在可搜索的选项卡式界面(又称“Cheers View Controller”)中显示歌曲列表。这里有两个标签,每个标签对应我的欢呼声。我有一个侧边栏,可以让你选择菜单选项,其中一个叫做“我的收藏夹”。它使用SWREVEL控制器(也称为“侧栏”)。我试图让用户在比赛前标记最喜欢的欢呼声,然后在休息区使用侧栏导航快速参考他们最喜欢的歌曲列表 我的应用程序的初始视图控制器是显示视图控制器 //Copy favorite cheers into array NSPred
//Copy favorite cheers into array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.favorite contains[c] %@",@"Yes"];
NSArray *tempArray = [self.cheersArray filteredArrayUsingPredicate:predicate];
self.myFavoriteCheersArray = [NSMutableArray arrayWithArray:tempArray];
前视图(sw_front)是选项卡栏控制器,在应用程序首次启动时显示Cheers视图控制器。数据以CSV文件的形式加载到Cheers视图控制器的ViewDidLoad中的数组中
//Copy favorite cheers into array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.favorite contains[c] %@",@"Yes"];
NSArray *tempArray = [self.cheersArray filteredArrayUsingPredicate:predicate];
self.myFavoriteCheersArray = [NSMutableArray arrayWithArray:tempArray];
后部控制器(sw_rear)是定义我的应用程序菜单选项的侧栏。我的侧边栏上的一个菜单选项称为“我的收藏夹”。正是这个侧边栏在概念上让我感到困惑。下面是我故事板相关区域的快照
情节提要快照:
事实证明,作为一名新用户,我无法发布图像(
目标
我正在尝试从“我的收藏夹”视图控制器访问“欢呼”视图控制器中的“最喜爱的欢呼”数组。当前从“欢呼”视图控制器到“我的收藏夹”没有分段,似乎在这里定义分段在概念上是错误的
我不想使用单件或其他解决方法,我宁愿以正确的方式构建我的应用程序。一个简单的解决方案是将我的收藏夹移动到选项卡栏控制器的另一个选项卡上。虽然我可以更改设计,事情可能会更简单,但我觉得我缺少了一个有价值的概念,可以在以后的应用程序中使用。
我意识到已经有很多关于将数据从一个控制器传递到另一个控制器的帖子。我已经阅读了我认为是所有的帖子,但我还没有能够成功地将解决方案整合到SWRevealController实现中。因此,很明显,我对机制或概念(或两者)感到困惑.我读到的大多数帖子都涉及两个控制器之间的分段或传递字段,这两个控制器具有分段或某种关系
力学问题:
目前,我在主选项卡界面中的“表格视图”控制器的ViewDiLoad中准备收藏夹数组。我在加载包含CSV文件中所有歌曲的主数组后立即进行了准备。我想我可以通过某种方式将其传递到“我的收藏夹”表格视图控制器,或从“我的收藏夹”表格视图控件访问它莱尔
//Copy favorite cheers into array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.favorite contains[c] %@",@"Yes"];
NSArray *tempArray = [self.cheersArray filteredArrayUsingPredicate:predicate];
self.myFavoriteCheersArray = [NSMutableArray arrayWithArray:tempArray];
这对我来说很棘手-当用户单击按钮显示侧边栏,然后单击我的收藏夹时:
边栏的prepareForSegue将使用以下命令执行:
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
// Get the selected row and identify the destination controller you are sending to
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
// Set the title of navigation bar by using the menu items
destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString];
if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[dvc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
我试图将其添加到prepareForSegue的末尾,以某种方式允许我的收藏夹访问数组…但是我在侧边栏中识别Cheers视图控制器中的属性时遇到了问题
if([segue.identifier isEqualToString:@"myFavorites"]) {
//Get the Favorites View Controller (which has a navigation controller)
MyFavoritesViewController *myFavoritesViewController = [[MyFavoritesViewController alloc] init];
myFavoritesViewController = (MyFavoritesViewController*)destViewController.topViewController;
//THIS BELOW PART IS SHAKEY AT BEST
//Get the Cheers View Controller (which is part of a tabbed view controller)
SWRevealViewController *frontViewController = [[SWRevealViewController alloc] init];
CheersViewController *cheersViewController = (CheersViewController*)frontViewController.frontViewController;
//Pass the Favorites Array -- The below code gives me an error that the property I set on the CheersViewController is not found.
rankingViewController.favoriteCheers = CheersViewController.myFavoriteCheersArray;
}
}
我喜欢评论的概念问题:
下面是这些问题的解决方法。它们是有效的——至于它们是否是最好的,我会让你知道我了解更多。咧嘴笑 问题1:从AppDelegate didFinishLaunchingWithOptions中的plist、CSV或任何其他源文件加载初始数据集。例如:
//Read in from JSON file
NSError *error;
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Cheers" ofType:@"json"]];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
self.cheersArray = json[@"cheers"];
//Instantiate initial view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
CMViewController *cmViewController = [storyboard instantiateInitialViewController];
//Pass array of data into a table view controller
cmViewController.tableData = (NSArray *)self.tableData;
此数组可以是不可变的(即NSArray),您可以将其强制转换为视图控制器,如果需要,视图控制器可以将其作为NSMutableArray进行操作。例如:
//Read in from JSON file
NSError *error;
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Cheers" ofType:@"json"]];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
self.cheersArray = json[@"cheers"];
//Instantiate initial view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
CMViewController *cmViewController = [storyboard instantiateInitialViewController];
//Pass array of data into a table view controller
cmViewController.tableData = (NSArray *)self.tableData;
这假定视图控制器具有可用于将数据传递到的属性
如果您有一个JSON文件,并且需要它是一个PLIST,那么Mac上有一个很棒的通过终端的实用程序
plutil -convert xml1 InputFileName.json -o OutputFileName.plist
当你像我一样学习时,你可能会意识到行数较少的plist比JSON文件更受欢迎,并且在保存数据更改时(如果你的应用程序允许的话)更容易处理(个人意见)
问题2:从学术角度来看,在应用启动时加载侧边栏在概念上是错误的。它应该只在第一次使用时加载
问题3:与问题2类似,最好在第一次请求时将收藏夹数组传递给需要它的视图控制器(即用户按下“收藏夹”选项卡)。因为从技术上讲,收藏夹只是mas子集的特定视图