UIDatepicker,默认情况下不返回当前时间

时间:2013-10-25 06:49:25

标签: iphone uidatepicker

在我的Iphone应用程序中创建剩余部分,在UIDate选择器中,默认情况下不返回当前时间。这是我的代码,请帮帮我。

我的viewController.m文件:

- (void)viewDidLoad
{


    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    UIBarButtonItem *add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(addRemainder)];
    self.navigationItem.rightBarButtonItem=add;

    //TextField1...............................................................................................................   

    self.title=@"Add Remainder";


    textField1 = [[UITextField alloc]initWithFrame:CGRectMake(20, 60, 280, 34)];
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField1.delegate = self;
    textField1.placeholder = @"Enter the Reminder name";
    [self.view addSubview:textField1];
    //datePicker&TextField2....................................................................................................



    datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0,210, self.view.frame.size.width, self.view.frame.size.height)]; 

    [datePicker addTarget:self action:@selector(actionDone) forControlEvents:UIControlEventValueChanged];


    [self.view addSubview:datePicker];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 





//UserDefaults.............................................................................................................

       NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
       remainder = [[NSMutableArray alloc]init];
       TimeDate = [[NSMutableArray alloc]init];

    if ([ud objectForKey:@"remainder"]) 
    {
        remainder = [[ud objectForKey:@"remainder"]mutableCopy];
        TimeDate = [[ud objectForKey:@"TimeDate"]mutableCopy];
    }



    //to make the fields visible when we try to edit it............................................................................

    if ([remainderr isEqualToString:@"edit"])
    {

        self.title = @"Reminder Edit";

        textField1.text = [remainder objectAtIndex:[index integerValue]];
        str = [TimeDate objectAtIndex:[index integerValue]];
    }
}

#pragma mark - doneClicked

//DoneButton.................................................................................................................

-(void)actionDone
{

    date = datePicker.date;
    NSDateFormatter *dateform=[[NSDateFormatter alloc]init];
    [datePicker setDate:[NSDate date]];
    dateform.dateFormat = @"dd-MM-YYYY HH:mm:SS";
    str=[dateform stringFromDate:date];
}



#pragma mark - AddRemainder Notifications

//addButton.................................................................................................................

-(void)addRemainder
{
    //LocalNotification.........................................................................................................
     notification = [[UILocalNotification alloc] init]; 
    notification.fireDate = date;
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    //NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    if ([remainderr isEqualToString:@"edit"])
    {
        [remainder replaceObjectAtIndex:[index integerValue] withObject:textField1.text];
        [TimeDate replaceObjectAtIndex:[index integerValue] withObject: str];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if([remainderr isEqualToString:@"delete"])
    {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];

    }
    else 
    {
        if((textField1.text!=NULL)&&( date!=NULL))
           {
        [remainder addObject:textField1.text];

        [TimeDate addObject: str];
        [self.navigationController popViewControllerAnimated:YES];
           }
        else
        {

            UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Alert !" message:@"Please Give a title for your Remainder " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [myAlert show];


        }
    } 


//Adding objects............................................................................................................


    //NSLog(@"%@   %@", remainder, TimeDate);

    [self.delegate store:remainder :TimeDate];

     NSLog(@"The notifications is \n %@",notification);





}



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;   
}
@end

谢谢

1 个答案:

答案 0 :(得分:0)

按如下方式制作你的VoidAction,

-(void)actionDone
{


    NSDateFormatter *dateform=[[NSDateFormatter alloc]init];
    dateform.dateFormat = @"dd-MM-yyyy HH:mm";
    NSString *formattedDate = [dateform stringFromDate:datePicker.date];
    date=[dateform dateFromString:formattedDate];
    str=[dateform stringFromDate:date];    
}

注意:发布问题时请相关。谢谢。