2015-06-03 39 views
2

我在我的視圖控制器類中創建自定義日期選擇器。那麼我需要在另一個視圖控制器類中的datepicker。所以我想創建任何視圖控制器類可以使用並顯示日期的通用日期選擇器。那我該如何使用它。請分享代碼。如何顯示來自不同類的datepicker方法調用?

我用這個代碼:

actionSheet = [[UIActionSheet alloc] initWithTitle:@"Date" delegate:self 
             cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 
    [actionSheet setBackgroundColor:[UIColor grayColor ]]; 
    [actionSheet setFrame:CGRectMake(0, 470, 375, 200)]; 
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake (0, 10, 300, 100)]; 
    dateFormatter = [[NSDateFormatter alloc] init]; 
    datePicker.datePickerMode = UIDatePickerModeDate; 
    [dateFormatter setDateFormat:@"MM/dd/yyyy"]; 
    [datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; 
    [actionSheet addSubview:datePicker]; 
    UIToolbar *toolbarPicker = [[UIToolbar alloc] init]; 
    toolbarPicker.backgroundColor=[UIColor grayColor]; 
    // toolbarPicker.barStyle=UIBarStyleBlackOpaque; 
    [toolbarPicker sizeToFit]; 
    NSMutableArray *itemsBar = [[NSMutableArray alloc] init]; 
    //calls DoneClicked 


    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked)]; 
    [itemsBar addObject:btnDone]; 
    [toolbarPicker setItems:itemsBar animated:YES]; 
    [actionSheet addSubview:toolbarPicker]; 
    [self.view addSubview:actionSheet]; 
} 

-(IBAction)dateChanged{ 
     self.dateEntry.text = [dateFormatter stringFromDate:[datePicker date]]; 
} 


-(IBAction)doneClicked{ 
    [actionSheet removeFromSuperview]; 
    self.dateEntry.userInteractionEnabled = YES; 
} 

回答

1

創建自定義類的UIView/NSObject,添加您的邏輯在那裏&然後實現委託設計模式來管理它。

讓我知道你是否需要更多的澄清相同。

+0

是的,如果你更清楚,我會感謝你。 – Tinku

+0

當然,請參閱此鏈接https://github.com/ryanmaxwell/UIActionSheet-Blocks其基於塊的行動表,其中一切都管理你不需要擔心任何事情..如果你有任何疑問,然後讓我知道 –

+0

希望它解決了你的問題。 –

相關問題