1

我正在尝试使用 github 上提供的 ECSlidingViewController:

https://github.com/edgecase/ECSlidingViewController

无论如何,我想知道如何将数据从菜单视图控制器推送到示例表视图控制器,因为没有 segue。导航控制器通过故事板实例化,但我看不到将任何数据从视图推送到另一个视图的方法。

我现在与代表一起尝试过,但似乎我的代表由于某种我不知道的原因没有解雇。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
4

2 回答 2

1

我建议使用代表。这是一个示例:委托示例

于 2013-04-05T17:30:31.750 回答
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];
于 2013-04-07T17:37:22.090 回答