Created
September 12, 2012 15:04
-
-
Save Jarada/3707234 to your computer and use it in GitHub Desktop.
myLauncher - adding a myLauncherItem via a View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an example coding gist for use on myLauncher Google Groups to explain how to add an item to myLauncherView from another view | |
/* | |
RootViewController - represents the RootViewController that extends MyLauncherViewController | |
*/ | |
@implementation RootViewController | |
-(void)addLauncherItem { | |
// We'll load our new view to allow a user to choose the item to add | |
MyNewViewController *nvc = [[MyNewViewController alloc] initWithNibName:nil bundle:nil]; // Change as needed | |
[nvc setLauncherView:self.launcherView]; | |
[self presentModalViewController:nvc animated:YES]; | |
} | |
@end | |
/* | |
MyNewViewController - represents the View Controller that shows a list of available items to add | |
*/ | |
@interface MyNewViewController | |
@property (nonatomic, strong) MyLauncherView *launcherView; | |
@end | |
@implementation MyNewViewController | |
@synthesize launcherView = _launcherView; | |
/* | |
Add's an item to myLauncherView (first to the end of the last page, then to another page, then to any space available) | |
*/ | |
- (void)addItem:(MyLauncherItem *)item { | |
NSMutableArray *pages = [[self.launcherView pages] copy]; | |
// Get the last page, check it's size and add to it if possible | |
if ([[pages objectAtIndex:[pages count] - 1] count] < [self.launcherView maxItemsPerPage]) { | |
NSMutableArray *page = [pages objectAtIndex:[pages count] - 1]; | |
[page addObject:item]; | |
[self.launcherView setPages:pages animated:NO]; | |
return; | |
} | |
// Next, check if we can add it to the end | |
if ([pages count] < [self.launcherView maxPages]) { | |
NSMutableArray *page = [[NSMutableArray alloc] init]; | |
[page addObject:item]; | |
[pages addObject:page]; | |
[self.launcherView setPages:pages animated:NO]; | |
return; | |
} | |
// Finally, go back and see if we can add it anywhere there is room | |
for (int x = [pages count] - 2; x >= 0; x--) { | |
if ([[pages objectAtIndex:x] count] < [self.launcherView maxItemsPerPage]) { | |
[[pages objectAtIndex:x] addObject:item]; | |
[self.launcherView setPages:pages animated:NO]; | |
break; | |
} | |
} | |
// And dismiss this view! | |
[self.presentingViewController dismissModalViewControllerAnimated:YES]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment