I Have this class:
#class UIImagePicker;
#protocol UIImagePickerDelegate<NSObject>
#required
- (void)didFinishChooseImage:(UIImagePicker *)picker withImage:(UIImage*)img;
#end
#interface UIImagePicker : NSObject <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIImagePickerController *imgPicker;
}
#property (assign) int type;
#property (nonatomic, retain) UIImagePickerController *imgPicker;
#property (nonatomic,assign) id <UIImagePickerDelegate> delegate;
And this is how i am alloc the imgPicker:
-(void)showImagePicker{
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsEditing = YES;
imgPicker.delegate = self;
//Trigger get photo from library function
if (self.type == 0) {
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else {
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
}
And this is how i release the imgPicker:
- (void)dealloc
{
[imgPicker release],imgPicker = nil;
[super dealloc];
}
Did the release in the dealloc is ok? or i need to add another release because the imgPicker is with retain property??
I am doing some exercises with delegate, but now I have a problem with a UIView. This is my storyboard
I want to change the color of the UIView with 3 UISliders. The range of UISliders is from 0 to 255.
And this is my code:
ColorField is the UIView custom class
ColorField.h
#import <UIKit/UIKit.h>
#protocol ColorFieldDelegate <NSObject>
-(NSArray *)giveMeColors;
#end
#interface ColorField : UIView
#property (nonatomic , weak) IBOutlet id<ColorFieldDelegate> delegate;
#end
ColorField.m
#import "ColorField.h"
#implementation ColorField
#synthesize delegate = _delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
NSArray *arrayOfColors = [self.delegate giveMeColors];
int red = [[arrayOfColors objectAtIndex:0] intValue];
int green = [[arrayOfColors objectAtIndex:1] intValue];
int blue = [[arrayOfColors objectAtIndex:2] intValue];
NSLog(#"Red --> %d" ,red);
NSLog(#"Green --> %d" ,green);
NSLog(#"Blue --> %d \n\n" ,blue);
self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
#end
ColorViewController.h
#import <UIKit/UIKit.h>
#import "ColorField.h"
#interface ColorViewController : UIViewController <ColorFieldDelegate>
#property (nonatomic) IBOutlet ColorField *colorField;
#property (weak, nonatomic) IBOutlet UISlider *redSlider;
#property (weak, nonatomic) IBOutlet UISlider *greenSlider;
#property (weak, nonatomic) IBOutlet UISlider *blueSlider;
#end
ColorViewController.m
#import "ColorViewController.h"
#interface ColorViewController ()
#property (nonatomic) double redQuantity;
#property (nonatomic) double greenQuantity;
#property (nonatomic) double blueQuantity;
#end
#implementation ColorViewController
#synthesize colorField = _colorField;
#synthesize redSlider;
#synthesize greenSlider;
#synthesize blueSlider;
#synthesize redQuantity;
#synthesize blueQuantity;
#synthesize greenQuantity;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.colorField setDelegate:self];
[self.colorField setNeedsDisplay];
self.redQuantity = 125.0;
self.blueQuantity = 125.0;
self.greenQuantity = 125.0;
[self.colorField setNeedsDisplay];
// Do any additional setup after loading the view, typically from a nib.
}
-(ColorField *)colorField
{
if (_colorField == nil) {
_colorField = [[ColorField alloc] init];
}
return _colorField;
}
- (void)viewDidUnload
{
[self setColorField:nil];
[self setRedSlider:nil];
[self setGreenSlider:nil];
[self setBlueSlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(IBAction)changeRedQuantity:(UISlider *)sender
{
//methods of UISliders
self.redQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeBlueQuantity:(UISlider *)sender
{
self.blueQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeGreenQuantity:(UISlider *)sender
{
self.greenQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(NSArray *)giveMeColors
{
NSNumber *redNumber = [NSNumber numberWithDouble:self.redQuantity];
NSNumber *greenNumber = [NSNumber numberWithDouble:self.greenQuantity];
NSNumber *blueNumber = [NSNumber numberWithDouble:self.blueQuantity];
NSArray *array = [[NSArray alloc] initWithObjects:redNumber, greenNumber,blueNumber, nil];
return array;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
BUT...this is the result: It show me only RED, GREEN and BLUE colors without gradation, for example: RGB (255,0,75) is THIS
and not THIS:
I don't know with it can't show me gradation...
Thanks!!
Marco Manzoni
You have to divide your slidervalues by 255.0.
[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
colorWithRed only accepts values 0.0-1.0
I have a tab controller with 2 tabs "My information" and "their Information", which is just a bunch of text boxes. I want a Navigation controller at the top so I can go back, but the back button won't show up... here is my code:
Button pressed to show Form:
-(IBAction)onIncidentAidFormPressed:(id)sender {
FormController *aidForm = [[FormController alloc] initWithNibName:#"formController" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:aidForm animated:YES];
}
In FormController.m - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Aid Form", #"Aid Form title");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithNibName:#"navController" bundle:nil];
[navController pushViewController:viewController1 animated:YES];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return self;
The tabs show up correctly, and the navigation bar is there but no back button is shown to get back.
Edit: Here are the files: IncidentAidStartViewController.h
#import <UIKit/UIKit.h>
#interface IncidentAidStartViewController : UIViewController {
IBOutlet UIButton *incidentAidForm;
IBOutlet UIButton *emergencyContacts;
}
-(IBAction)onIncidentAidFormPressed:(id)sender;
#property (strong, nonatomic) IncidentAidStartViewController *detailViewController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
IncidentAidStartViewController.m
#import "IncidentAidStartViewController.h"
#import "IncidentAidForm.h"
#implementation IncidentAidStartViewController
#synthesize detailViewController = _detailViewController;
#synthesize managedObjectContext = __managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Incident Aid", #"Incident Aid title");
}
return self;
}
#pragma mark - Button handlers
-(IBAction)onIncidentAidFormPressed:(id)sender {
IncidentAidForm *aidForm = [[IncidentAidForm alloc] initWithNibName:#"IncidentAidForm" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:nil];
[self.navigationController pushViewController:aidForm animated:YES];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
IncidentAidForm.h
#import <UIKit/UIKit.h>
#interface IncidentAidForm : UIViewController <UITabBarControllerDelegate> {
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#property (strong, nonatomic) IncidentAidForm *detailViewController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
IncidentAidForm.m
#import "IncidentAidForm.h"
#import "MyInformationController.h"
#import "TheirInformationController.h"
#implementation IncidentAidForm
#synthesize detailViewController = _detailViewController;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Incident Aid Form", #"Incident Aid Form title");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
MyInformationController.h
#import <UIKit/UIKit.h>
#interface MyInformationController : UIViewController {
IBOutlet UIScrollView *scrollView;
IBOutlet UITextField *firstName;
IBOutlet UITextField *lastName;
IBOutlet UITextField *phoneNumber;
IBOutlet UITextField *address;
IBOutlet UITextField *carMake;
IBOutlet UITextField *carModel;
IBOutlet UITextField *carYear;
IBOutlet UITextField *licensePlate;
IBOutlet UITextField *insuranceCarrier;
IBOutlet UITextField *insurancePolicy;
IBOutlet UIButton *backgroundButton;
}
#property(nonatomic, retain) UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UITextField *firstName;
#property (nonatomic, retain) IBOutlet UITextField *lastName;
#property (nonatomic, retain) IBOutlet UITextField *phoneNumber;
#property (nonatomic, retain) IBOutlet UITextField *address;
#property (nonatomic, retain) IBOutlet UITextField *carMake;
#property (nonatomic, retain) IBOutlet UITextField *carModel;
#property (nonatomic, retain) IBOutlet UITextField *carYear;
#property (nonatomic, retain) IBOutlet UITextField *licensePlate;
#property (nonatomic, retain) IBOutlet UITextField *insuranceCarrier;
#property (nonatomic, retain) IBOutlet UITextField *insurancePolicy;
- (void) animateTextField: (UITextField*) textField up: (BOOL) up;
-(IBAction)hideKeyboard;
#end
MyInformationController.m
#import "MyInformationController.h"
#implementation MyInformationController
#synthesize scrollView, firstName, lastName, phoneNumber, address, carMake, carModel, carYear, licensePlate,
insuranceCarrier, insurancePolicy;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"My Information", #"My Information");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
// Do any additional setup after loading the view, typically from a nib.
firstName.delegate = self;
lastName.delegate = self;
phoneNumber.delegate = self;
address.delegate = self;
carMake.delegate = self;
carModel.delegate = self;
carYear.delegate = self;
licensePlate.delegate = self;
insuranceCarrier.delegate = self;
insurancePolicy.delegate = self;
[scrollView setContentSize:self.view.frame.size];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//[self animateTextField: textField up: YES];
scrollView.frame = CGRectMake(0,44,320,200); //44:NavigationBar ; 200: Keyoard
[scrollView scrollRectToVisible:textField.frame animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//[self animateTextField: textField up: NO];
if (textField.tag == 10) {
scrollView.frame = CGRectMake(0,44,320,416); //original setup
// [textField resignFirstResponder];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
switch (textField.tag) {
case 1:
[lastName becomeFirstResponder];
break;
case 2:
[phoneNumber becomeFirstResponder];
break;
case 3:
[address becomeFirstResponder];
break;
case 4:
[carMake becomeFirstResponder];
break;
case 5:
[carModel becomeFirstResponder];
break;
case 6:
[carYear becomeFirstResponder];
break;
case 7:
[licensePlate becomeFirstResponder];
break;
case 8:
[insuranceCarrier becomeFirstResponder];
break;
case 9:
[insurancePolicy becomeFirstResponder];
break;
case 10:
[textField resignFirstResponder];
break;
default:
break;
}
return TRUE;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)hideKeyboard {
[self.firstName resignFirstResponder];
[self.lastName resignFirstResponder];
[self.phoneNumber resignFirstResponder];
[self.address resignFirstResponder];
[self.carMake resignFirstResponder];
[self.carModel resignFirstResponder];
[self.carYear resignFirstResponder];
[self.licensePlate resignFirstResponder];
[self.insurancePolicy resignFirstResponder];
[self.insuranceCarrier resignFirstResponder];
scrollView.frame = CGRectMake(0,44,320,416); //original setup
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
So far I know this is a good way to add ur views to tabbar. So try this
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
To add back bar item , below ine is also used.
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
Please modify your method like this
-(IBAction)onIncidentAidFormPressed:(id)sender
{
FormController *aidForm = [[FormController alloc] initWithNibName:#"formController" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:nil];
[self.navigationController pushViewController:aidForm animated:YES];
}
you should replace your code with the following:
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
UINavigationController *navController2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
let me know any thing else you need........
I'm facing an annoying issue and I can't find out why.
I have a UIViewController I present in modal like that :
interviewsViewController *interviewsVC = [[interviewsViewController alloc] initWithNibName:nil bundle:nil];
[interviewsVC setManagedObjectContext:_managedObjectContext];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:interviewsVC];
[interviewsVC release];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
Then when I dismiss the view controller like this :
- (void)dismissViewController
{
[self dismissModalViewControllerAnimated:YES];
}
The dealloc gets called :
- (void)dealloc
{
[_managedObjectContext release];
[_interviewsArray release];
[scrollView release];
[pageControl release];
}
Once the view controller is dismissed, I send an memory warning via the iPhone Simulator Menu and the viewdidunload method gets called :
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}
But there's always an error EXC_BAD_ACCES on the self.scrollView = nil ... More specifically at this line :
#synthesize scrollView;
And I can't find out why ?
If I add a breakpoint on the line above this one, the scrollView is not a zombie or equal to 0x0 ...
Do you have an idea ?
PS : Here's the header :
#import <UIKit/UIKit.h>
#interface interviewsViewController : UIViewController <UIScrollViewDelegate>
{
NSManagedObjectContext *_managedObjectContext;
NSMutableArray *_interviewsArray;
NSUInteger _fetchOffset;
CGFloat _lastXValue;
}
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) NSMutableArray *interviewsArray;
//IBOutlet
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
And I set the delegate of the scrollview in the XIB (not in the code).
You need to release properly in dealloc-
Use-
- (void)dealloc {
[_managedObjectContext release];
[_interviewsArray release];
self.scrollView = nil;
self.pageControl = nil;
[super dealloc];
}
ViewDidUnload also be used as that will be helpfull in case of low memory warnings.
NEW CODE
DatePickerViewController.h
#import <UIKit/UIKit.h>
#protocol DatePickerViewControllerDelegate;
#interface DatePickerViewController : UIViewController {
IBOutlet UIDatePicker *datePicker;
id<DatePickerViewControllerDelegate> delegate;
}
#property (retain) IBOutlet UIDatePicker *datePicker;
#property (assign) id<DatePickerViewControllerDelegate> delegate;
NSInteger buttonPressed;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
- (IBAction)doneButtonPressed:(id)sender;
#end
#protocol DatePickerViewControllerDelegate <NSObject>
#optional
-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate;
#end
DatePickerViewController.m
#import "DatePickerViewController.h"
#implementation DatePickerViewController
#synthesize datePicker, delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title = #"Date Picker";
}
return self;
}
- (void)viewDidLoad {
NSLog(#"Date Picker. viewDidLoad");
[super viewDidLoad];
double days = 2.0f;
datePicker.date = [NSDate dateWithTimeIntervalSinceNow:60.0f * 60.0f * 24.0f * days];
}
//-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate;
- (IBAction)doneButtonPressed:(id)sender
{
if ([self.delegate respondsToSelector:#selector(datePickerViewController:didChooseDate:)]) {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [dateFormatter stringFromDate:[datePicker date]];
[self.delegate datePickerViewController:self didChooseDate:dateString];
[self dismissModalViewControllerAnimated:YES];
}
}
- (void)dealloc {
[datePicker release];
[super dealloc];
}
#end
DatePickerViewController2.h
#import <UIKit/UIKit.h>
#protocol DatePickerViewController2Delegate;
#interface DatePickerViewController2 : UIViewController {
IBOutlet UIDatePicker *datePicker2;
id<DatePickerViewController2Delegate> delegate;
}
#property (retain) IBOutlet UIDatePicker *datePicker2;
#property (assign) id<DatePickerViewController2Delegate> delegate;
NSInteger buttonPressed2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
- (IBAction)doneButtonPressed2:(id)sender;
#end
#protocol DatePickerViewController2Delegate <NSObject>
#optional
-(void)datePickerViewController2:(DatePickerViewController2 *)controller didChooseDate:(NSString *)chosenDate;
#end
DatePickerViewController2.m
#import "DatePickerViewController2.h"
#implementation DatePickerViewController2
#synthesize datePicker2, delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title = #"Date Picker2";
}
return self;
}
- (void)viewDidLoad {
NSLog(#"Date Picker2. viewDidLoad");
[super viewDidLoad];
double days = 2.0f;
datePicker2.date = [NSDate dateWithTimeIntervalSinceNow:60.0f * 60.0f * 24.0f * days];
}
//-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate;
- (IBAction)doneButtonPressed2:(id)sender
{
if ([self.delegate respondsToSelector:#selector(datePickerViewController2:didChooseDate:)]) {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [dateFormatter stringFromDate:[datePicker2 date]];
[self.delegate datePickerViewController2:self didChooseDate:dateString];
[self dismissModalViewControllerAnimated:YES];
}
}
- (void)dealloc {
[datePicker2 release];
[super dealloc];
}
#end
DatePickerModalExampleAppDelegate.h
#import <UIKit/UIKit.h>
#class DatePickerModalExampleViewController;
#interface DatePickerModalExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
DatePickerModalExampleViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet DatePickerModalExampleViewController *viewController;
#end
DatePickerModalExampleAppDelegate.m
#import "DatePickerModalExampleAppDelegate.h"
#import "DatePickerModalExampleViewController.h"
#implementation DatePickerModalExampleAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
DatePickerModalExampleViewController.h
#import <UIKit/UIKit.h>
#import "DatePickerViewController.h"
#import "DatePickerViewController2.h"
#interface DatePickerModalExampleViewController : UIViewController <DatePickerViewControllerDelegate> {
IBOutlet UIButton *button;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
}
#property(nonatomic, retain) IBOutlet UIButton *button;
#property(nonatomic, retain) IBOutlet UIButton *button2;
#property(nonatomic, retain) IBOutlet UIButton *button3;
-(IBAction)buttonPressed:(id)sender;
-(IBAction)buttonPressed2:(id)sender;
#end
DatePickerModalExampleViewController.m
#import "DatePickerModalExampleViewController.h"
#implementation DatePickerModalExampleViewController
#synthesize button;
#synthesize button2;
#synthesize button3;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
-(IBAction)buttonPressed:(id)sender{
NSLog(#"I was pressed");
buttonPressed = ((UIButton *)sender).tag;
DatePickerViewController *datePickerViewController = [[DatePickerViewController alloc] initWithNibName:#"DatePickerViewController" bundle:nil];
datePickerViewController.delegate = self;
[self presentModalViewController:datePickerViewController animated:YES];
[datePickerViewController release];
switch (((UIButton*)sender).tag)
{
case 100001:
NSLog(#"Button 1 was pressed");
//some code
break;
case 100002:
NSLog(#"Button 2 was pressed");
//some code
break;
}
}
-(IBAction)buttonPressed2:(id)sender{
NSLog(#"I was pressed2");
buttonPressed2 = ((UIButton *)sender).tag;
DatePickerViewController2 *datePickerViewController2 = [[DatePickerViewController2 alloc] initWithNibName:#"DatePickerViewController2" bundle:nil];
datePickerViewController2.delegate = self;
[self presentModalViewController:datePickerViewController2 animated:YES];
[datePickerViewController2 release];
switch (((UIButton*)sender).tag)
{
case 100003:
NSLog(#"Button 3 was pressed");
//some code
break;
}
}
-(void)viewDidLoad{
[super viewDidLoad];
self.button.tag = 100001;
self.button2.tag = 100002;
self.button3.tag = 100003;
buttonPressed = -1;
buttonPressed2 = -1;
}
-(void)datePickerViewController:(DatePickerViewController *)controller didChooseDate:(NSString *)chosenDate{
NSLog(#"Chosen Date as String: %#", chosenDate );
if (buttonPressed == -1)
return;
UIButton *buttonToSet = (UIButton*)[self.view viewWithTag:buttonPressed];
buttonPressed = -1;
[buttonToSet setTitle: chosenDate forState: UIControlStateNormal];
[self dismissModalViewControllerAnimated:YES];
}
-(void)datePickerViewController2:(DatePickerViewController2 *)controller didChooseDate:(NSString *)chosenDate{
NSLog(#"Chosen Date as String: %#", chosenDate );
if (buttonPressed2 == -1)
return;
UIButton *buttonToSet = (UIButton*)[self.view viewWithTag:buttonPressed2];
buttonPressed2 = -1;
[buttonToSet setTitle: chosenDate forState: UIControlStateNormal];
[self dismissModalViewControllerAnimated:YES];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[button3 release];
[button2 release];
[button release];
[super dealloc];
}
#end
Hello you can set the property "tag" on button to identify it
Uncomment
- (void)viewDidLoad {
[super viewDidLoad];
}
And type :
- (void)viewDidLoad {
[super viewDidLoad];
self.button.tag = 100001;
self.button2.tag = 100002;
}
then
turn in DatePickerModalExampleViewController.h
(IBAction)buttonPressed to (IBAction)buttonPressed:(id)sender;
Then turn in DatePickerModalExampleViewController.m
-(IBAction)buttonPressed to -(IBAction)buttonPressed:(id)sender
and in DatePickerModalExampleViewController.m this method could be like that :
-(IBAction)buttonPressed:(id)sender
{
switch(((UIButton*)sender).tag)
{
case 100001
NSLog(#"Button 1 was pressed");
DatePickerViewController *datePickerViewController =[DatePickerViewController alloc] initWithNibName:#"DatePickerViewController" bundle:nil];
datePickerViewController.delegate = self;
[self presentModalViewController:datePickerViewController animated:YES];
[datePickerViewController release];
break;
case 100002 :
NSLog(#"Button 2 was pressed");
// some code
break;
}
}
As you use the interfacebuilder I'm not friend with it but you must relink your actions
I'm writting from my PC so without xcode some code can contains syntax error be carreful. but it's the main idea.
Use tag for both buttons and check the condition accordingly...