我正在尝试使用github上提供的ECSlidingViewController:
https://github.com/edgecase/ECSlidingViewController
无论如何,我想知道如何将数据从菜单视图控制器推送到样本表视图控制器,因为没有segue。导航控制器通过storybaord实例化,但我认为无法将任何数据从视图推送到另一个。
我现在和代表们一起试过但似乎我的代表不会因为某种原因而不知道。 TableView保持空白。我错过了什么?
请参阅以下代码:
//MenuviewController.h
#import <UIKit/UIKit.h>
#import "ECSlidingViewController.h"
@protocol TableData <NSObject>
- (void) someData:(NSString*)data;
@end
@interface MenuViewController : UIViewController <UITableViewDataSource, UITabBarControllerDelegate>{
__unsafe_unretained id <TableData> delegate;
}
@property (nonatomic,assign) id <TableData> delegate;
@end
//MenuViewController.m
#import "MenuViewController.h"
@interface MenuViewController()
@property (nonatomic, strong) NSArray *menuItems;
@end
@implementation MenuViewController
@synthesize menuItems, delegate;
- (void)awakeFromNib
{
self.menuItems = [NSArray arrayWithObjects:@"First", @"Second", @"Third", @"Navigation", nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@"%@Top", [self.menuItems objectAtIndex:indexPath.row]];
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
if ([delegate respondsToSelector:@selector(someData:)]) {
[delegate someData:@"Hello World"];
}
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
}];
}
@end
//SampleTableViewController.h
#import <UIKit/UIKit.h>
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
@interface SampleTableViewController : UITableViewController <UITableViewDataSource, UITabBarControllerDelegate, TableData>
- (IBAction)revealMenu:(id)sender;
@end
//SampleTableViewController.m
#import "SampleTableViewController.h"
@interface SampleTableViewController()
@property (nonatomic, strong) NSArray *sampleItems;
@end
@implementation SampleTableViewController
@synthesize sampleItems;
- (void)awakeFromNib
{
//self.sampleItems = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
}
- (void) someData:(NSString *)data{
sampleItems = [NSArray arrayWithContentsOfFile:data];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.sampleItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"SampleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.sampleItems objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)revealMenu:(id)sender
{
[self.slidingViewController anchorTopViewTo:ECRight];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
@end
答案 0 :(得分:1)
我建议使用代表。以下是一个示例:Delegate example
答案 1 :(得分:1)
我终于找到了。
这是一个例子。
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
MBFancyViewController *viewController = navigationController.viewControllers[0];
//or alternative
MBFancyViewController *viewController = (MBFancyViewController *)navigationController.topViewController;
// setup "inner" view controller
viewController.foo = bar;
[self presentViewController:navigationController animated:YES completion:nil];