开发者

Adding a 3rd button and have date picker for time

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:开发者_JAVA百科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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜