Sfoglia il codice sorgente

Merge pull request #42 from maniak-dobrii/master

Added ability to add UIViewController based user defined global entries.
Ryan Olson 11 anni fa
parent
commit
4b9385bee0

+ 10 - 1
Classes/Explorer Toolbar/FLEXManager.h

@@ -26,6 +26,15 @@
 /// @note This method must be called from the main thread.
 /// The objectFutureBlock will be invoked from the main thread and may return nil.
 /// @note The passed block will be copied and retain for the duration of the application, you may want to use __weak references.
-- (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id(^)(void))objectFutureBlock;
+- (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock;
+
+/// Adds an entry at the bottom of the list of Global State items. Call this method before this view controller is displayed.
+/// @param entryName The string to be displayed in the cell.
+/// @param viewControllerFutureBlock When you tap on the row, view controller returned by this block will be pushed on the navigation controller stack.
+/// @note This method must be called from the main thread.
+/// The viewControllerFutureBlock will be invoked from the main thread and may not return nil.
+/// @note The passed block will be copied and retain for the duration of the application, you may want to use __weak references.
+- (void)registerGlobalEntryWithName:(NSString *)entryName
+          viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock;
 
 @end

+ 18 - 0
Classes/Explorer Toolbar/FLEXManager.m

@@ -117,4 +117,22 @@
     [self.userGlobalEntries addObject:entry];
 }
 
+- (void)registerGlobalEntryWithName:(NSString *)entryName viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock
+{
+    NSParameterAssert(entryName);
+    NSParameterAssert(viewControllerFutureBlock);
+    NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
+
+    entryName = entryName.copy;
+    FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
+        return entryName;
+    } viewControllerFuture:^UIViewController *{
+        UIViewController *viewController = viewControllerFutureBlock();
+        NSCAssert(viewController, @"'%@' entry returned nil viewController. viewControllerFutureBlock should never return nil.", entryName);
+        return viewControllerFutureBlock();
+    }];
+
+    [self.userGlobalEntries addObject:entry];
+}
+
 @end

+ 37 - 1
Example/UICatalog/AAPLCatalogTableTableViewController.m

@@ -12,7 +12,7 @@
 #endif
 
 @interface AAPLCatalogTableTableViewController ()
-
+- (void)registerViewControllerBasedOption;
 @end
 
 @implementation AAPLCatalogTableTableViewController
@@ -22,6 +22,7 @@
     [super viewDidLoad];
     
 #if DEBUG
+    [self registerViewControllerBasedOption];
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"FLEX" style:UIBarButtonItemStylePlain target:self action:@selector(flexButtonTapped:)];
 #endif
 }
@@ -34,4 +35,39 @@
 #endif
 }
 
+- (void)registerViewControllerBasedOption
+{
+    // create UIViewController subclass
+    UIViewController *viewController = [[UIViewController alloc] init];
+
+    // fill it with some stuff
+    UILabel *infoLabel = [[UILabel alloc] init];
+    infoLabel.translatesAutoresizingMaskIntoConstraints = NO;
+    infoLabel.text = @"Add switches, notes or whatewer you wish to provide your testers with superpowers!";
+    infoLabel.numberOfLines = 0;
+    infoLabel.textAlignment = NSTextAlignmentCenter;
+
+    UIView *view = viewController.view;
+    view.backgroundColor = [UIColor whiteColor];
+    [view addSubview:infoLabel];
+
+    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[infoLabel]-0-|"
+                                                                 options:0
+                                                                 metrics:nil
+                                                                   views:NSDictionaryOfVariableBindings(infoLabel)]];
+
+    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[infoLabel]-0-|"
+                                                                 options:0
+                                                                 metrics:nil
+                                                                   views:NSDictionaryOfVariableBindings(infoLabel)]];
+
+
+
+    // return it in viewControllerFutureBlock
+    [[FLEXManager sharedManager] registerGlobalEntryWithName:@"Custom superpowers"
+                                   viewControllerFutureBlock:^id{
+                                       return viewController;
+                                   }];
+}
+
 @end