昨天完成了標籤欄,今天繼續在標籤欄上添加日期選取器。
要實現這個功能,需要一個輸出口和一個操作方法。要新增那麼就要先到頭文件中去聲明輸出口和操作方法接口。我是這樣理解的。
#import<UIKit/UIKit.h>
@interface BIDDatePickerViewController :UIViewController
@property(strong,nonatomic)IBOutletUIDatePicker *datePicker;
-(IBAction)buttonPressed;
@end
注意這個buttonPressed沒有參數。後面我會提到。
在庫中找到Date Picker
拖入到BIDDatePickerViewController.xib中找到屬性檢查器
選中Minimum Date 和Maximum Date。
拖入一個Button 點擊的時候彈出一個警告。
點擊button就是觸發Touch up Inside 事件將它關聯File‘s Owner中的buttonPressed。在將File’s Owner 關聯到日期選取器的datePicker輸出口。
接下啦就是填充代碼。
-(IBAction)buttonPressed
{
NSDate *selectd=[datePicker date];
NSString *message=[[NSStringalloc]initWithFormat:@"The date and time you selected is:%@",selectd];
UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"Date and Time Selected"message:messagedelegate:nilcancelButtonTitle:@"Yes,I did."otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
NSDate *now =[NSDate date];
[datePickersetDate:nowanimated:NO];
}
- (void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.datePicker=nil;
}
這樣運行就完成了日期選取器。因爲xcode有自動提示,剛開始沒注意,就把buttonPressed的參數帶上了。運行報錯,說時內存異常,看到下面提示時buttonPressed方法。然後修改方法重新關聯,仍然還是報這個錯,最後把Button刪除,重新創建和關聯。運行成功。