Skip to content
Browse files

- added delegate method similar to UITabBarControllerDelegate

  • Loading branch information...
1 parent df693d0 commit 3770f24636b9b335ec6e045ef1d666cd57d90bc2 @honcheng committed Oct 26, 2012
View
2 Demo/Demo.xcodeproj/project.pbxproj
@@ -82,6 +82,7 @@
F8938534163AD6FA0085C3BB /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
F893853A163AD8680085C3BB /* DemoMenuController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoMenuController.h; sourceTree = "<group>"; };
F893853B163AD8680085C3BB /* DemoMenuController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoMenuController.m; sourceTree = "<group>"; };
+ F8938557163ADFE40085C3BB /* PaperFoldMenuControllerDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PaperFoldMenuControllerDelegate.h; path = ../PaperFoldMenuController/PaperFoldMenuControllerDelegate.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -166,6 +167,7 @@
F89384EB163AD49E0085C3BB /* PaperFold */,
F89384C4163AD3BC0085C3BB /* PaperFoldMenuController.h */,
F89384C5163AD3BC0085C3BB /* PaperFoldMenuController.m */,
+ F8938557163ADFE40085C3BB /* PaperFoldMenuControllerDelegate.h */,
);
name = PaperFoldMenuController;
sourceTree = "<group>";
View
2 Demo/Demo/AppDelegate.h
@@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import "DemoMenuController.h"
-@interface AppDelegate : UIResponder <UIApplicationDelegate>
+@interface AppDelegate : UIResponder <UIApplicationDelegate, PaperFoldMenuControllerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) DemoMenuController *menuController;
View
7 Demo/Demo/AppDelegate.m
@@ -18,6 +18,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_menuController = [[DemoMenuController alloc] initWithMenuWidth:250.0];
+ [_menuController setDelegate:self];
[self.window setRootViewController:_menuController];
NSMutableArray *viewControllers = [NSMutableArray array];
@@ -38,10 +39,16 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[viewControllers addObject:thirdNavController];
[_menuController setViewControllers:viewControllers];
+ [_menuController setSelectedIndex:1];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
+- (void)paperFoldMenuController:(PaperFoldMenuController *)paperFoldMenuController didSelectViewController:(UIViewController *)viewController
+{
+ NSLog(@"%@", viewController);
+}
+
@end
View
2 PaperFoldMenuController/PaperFoldMenuController.h
@@ -33,12 +33,14 @@
#import <UIKit/UIKit.h>
#import "PaperFoldView.h"
+#import "PaperFoldMenuControllerDelegate.h"
@interface PaperFoldMenuController : UIViewController <PaperFoldViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) PaperFoldView *paperFoldView;
@property (nonatomic, strong) NSMutableArray *viewControllers;
@property (nonatomic, strong) UITableView *menuTableView;
@property (nonatomic, strong) UIView *contentView;
+@property (nonatomic, assign) id<PaperFoldMenuControllerDelegate> delegate;
/**
* Set and return the current view controller;
*/
View
23 PaperFoldMenuController/PaperFoldMenuController.m
@@ -96,6 +96,8 @@ - (void)setSelectedIndex:(NSUInteger)selectedIndex
{
_selectedIndex = selectedIndex;
+ [self.menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
+
UIView *lastView = (UIView*)[self.contentView viewWithTag:TAG_CURRENT_VIEWCONTROLLER];
if (lastView)
{
@@ -115,6 +117,12 @@ - (void)setSelectedIndex:(NSUInteger)selectedIndex
[self addChildViewController:_selectedViewController];
[self.contentView addSubview:_selectedViewController.view];
[_selectedViewController didMoveToParentViewController:self];
+
+ if (self.paperFoldView.state!=PaperFoldStateLeftUnfolded)
+ {
+ [self reloadMenu];
+ }
+
}
- (void)setSelectedViewController:(UIViewController *)selectedViewController
@@ -160,7 +168,20 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
{
if (tableView==self.menuTableView)
{
- [self setSelectedIndex:indexPath.row];
+ BOOL shouldSelect = YES;
+ if ([self.delegate respondsToSelector:@selector(paperFoldMenuController:shouldSelectViewController:)])
+ {
+ shouldSelect = [self.delegate paperFoldMenuController:self shouldSelectViewController:self.viewControllers[indexPath.row]];
+ }
+ if (shouldSelect)
+ {
+ [self setSelectedIndex:indexPath.row];
+ if ([self.delegate respondsToSelector:@selector(paperFoldMenuController:didSelectViewController:)])
+ {
+ [self.delegate paperFoldMenuController:self didSelectViewController:_selectedViewController];
+ }
+ }
+
}
}
View
42 PaperFoldMenuController/PaperFoldMenuControllerDelegate.h
@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2012 Muh Hon Cheng
+ * Created by honcheng on 26/10/12.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject
+ * to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
+ * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+ * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+ * IN CONNECTION WITH THE SOFTWARE OR
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * @author Muh Hon Cheng <honcheng@gmail.com>
+ * @copyright 2012 Muh Hon Cheng
+ * @version
+ *
+ */
+
+#import <Foundation/Foundation.h>
+
+@class PaperFoldMenuController;
+
+@protocol PaperFoldMenuControllerDelegate <NSObject>
+@optional
+- (BOOL)paperFoldMenuController:(PaperFoldMenuController *)paperFoldMenuController shouldSelectViewController:(UIViewController *)viewController;
+- (void)paperFoldMenuController:(PaperFoldMenuController *)paperFoldMenuController didSelectViewController:(UIViewController *)viewController;
+@end

0 comments on commit 3770f24

Please sign in to comment.
Something went wrong with that request. Please try again.