Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilomitra/374647 to your computer and use it in GitHub Desktop.
Save tilomitra/374647 to your computer and use it in GitHub Desktop.
//This will add the addButton on the rightmost corner of the UIToolbar (toolbar)
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTasks:)];
UIBarButtonItem *BtnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[toolbar setItems:[NSArray arrayWithObjects:BtnSpace,addButton, nil] animated:YES];
//The first example is adding a subview to your MainWindow nib so that it loads a nib from another view controller.
//The following code goes in the AppDelegate.m under the following method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
[window addSubview:switchViewController.view]; //replace switchViewController with your own viewController
[window makeKeyAndVisible];
return YES;
}
//The second example shows how to add subviews to a viewController
//This would go in the view controller you are adding subviews to.
- (void)viewDidLoad {
[super viewDidLoad];
//In this case, BlueViewController is a viewController that is being called in this viewController (switchViewController)
BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];
self.blueViewController = blueController;
[self.view insertSubview:blueController.view atIndex:0]; //index 0 determines zindex.
[blueController release];
}
//This example shows how to remove views (like the one above)
[yellowViewController.view removeFromSuperview];
//You can also check to see if the view of a given viewController is present using the following:
if (self.blueViewController.view.superview == nil) {
...
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test" message:@"clicked" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];
-(IBAction)switchViews:(id)sender {
//Create the second view (the one that is not showing)
if (self.yellowViewController == nil) {
//Supply your own nib name here ofcourse...
YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//The if/else below looks at which view is showing, so that it knows what to hide and what to show
//The blueviewcontroller is nil, which means the yellow one must be showing..
if (self.blueViewController.view.superview == nil) {
/*
* Instead of FlipFromRight, we could have used:
* UIViewAnimationTransitionFlipFromRight
* UIViewAnimationTransitionFlipFromLeft
* UIViewAnimationTransitionCurlUp
* UIViewAnimationTransitionCurlDown
*/
//Always have cache:YES unless you want the view look to change during the animation (like it does in Classics page flip)
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[blueViewController viewWillAppear:YES];
[yellowViewController viewWillDisappear:YES];
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
[blueViewController viewDidAppear:YES];
[yellowViewController viewDidDisappear:YES];
}
//The blue one is showing
else {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[yellowViewController viewWillAppear:YES];
[blueViewController viewWillDisappear:YES];
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
[yellowViewController viewDidAppear:YES];
[blueViewController viewDidDisappear:YES];
}
//Need this!!
[UIView commitAnimations];
}
// in view.didload
//addTaskViewController is the view shown inside the popover
//The popover is wrapped in a UINavigationController
//create add popover with a navigation bar attached
addTaskViewController = [[AddTaskViewController alloc] initWithNibName:@"AddTaskViewController" bundle:nil];
UINavigationController *addTaskNavController = [[UINavigationController alloc] initWithRootViewController:addTaskViewController];
UIPopoverController *addTaskPopOver = [[UIPopoverController alloc] initWithContentViewController:addTaskNavController];
self.addTaskPopOverController = addTaskPopOver;
addTaskPopOverController.delegate = self;
[addTaskPopOver release];
//Create a button to show the popover from (in view.didload)
//create add ('+') button to the righthand side of the toolbar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTasksButtonPressed:)];
UIBarButtonItem *BtnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[toolbar setItems:[NSArray arrayWithObjects:BtnSpace,addButton, nil] animated:YES];
[addButton release];
//Implement addTasksButtonPressed:(id)sender;
- (void) addTasksButtonPressed:(id)sender {
[self.addTaskPopOverController setPopoverContentSize:CGSizeMake(400, 250)];
[addTaskPopOverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTask:)];
- (void) addTasks:(id)sender {
//...
}
// Get the Gregorian calendar
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Get the date
NSDate* now = [NSDate date];
// Get the hours, minutes, seconds
NSDateComponents* nowHour = [cal components:NSHourCalendarUnit fromDate:now];
NSDateComponents* nowMinute = [cal components:NSMinuteCalendarUnit fromDate:now];
NSDateComponents* nowSecond = [cal components:NSSecondCalendarUnit fromDate:now];
// just some date
NSDate *fooDate = [NSDate date];
// setting units we would like to use in future
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
// creating NSCalendar object
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// extracting components from date
NSDateComponents *components = [calendar components:units fromDate:fooDate];
// getting our fooDate components. On at the time. Oh, and they're integers!
[components year];
[components month];
[components day];
[components weekday];
//This is your modal view controller
addProjectViewController = [[AddProjectViewController alloc] initWithNibName:@"AddProjectViewController" bundle:nil];
UINavigationController *modalNavigationController = [[UINavigationController alloc] initWithRootViewController:addProjectViewController];
//Set up Modal styles
modalNavigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
modalNavigationController.modalPresentationStyle = UIModalPresentationFormSheet;
//self refers to the view controller showing the modal view controller
[self presentModalViewController:modalNavigationController animated:YES];
//release controllers from memory
[addProjectViewController release];
[modalNavigationController release];
//To Dismiss, go to the Modal View Controller and call:
[self dismissModalViewControllerAnimated:YES];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment