2013-04-05 69 views
1

我想使用ECSlidingViewController它可在github:ECSlidingViewController推送數據表視圖

https://github.com/edgecase/ECSlidingViewController

反正我不知道如何從菜單視圖控制器將數據推送到樣品表視圖控制器,因爲沒有賽格。導航控制器通過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

我終於做到了。 – halloway4b 2013-04-07 17:34:46

回答

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]; 
1

我會建議使用委託。這裏是一個例子:Delegate example

+0

我已經嘗試了很多與代表。我也嘗試了很多其他方法,但我無法將菜單視圖控制器中的任何數據傳遞到示例視圖控制器。請幫忙。 – halloway4b 2013-04-06 20:19:50

+0

也許我錯過了它,但我沒有看到你的SampleTableViewController中的任何地方,你正在初始化委託。 OpeningsTableViewController * dvc = [segue destinationViewController]; dvc.delegate = self; – iOSGuru248 2013-04-08 14:13:01