添加考試功能總結

經過一個寒假的學習,我對oc和ios開發有了一定的瞭解,也僥倖成爲了實驗室的一員,學長三週前佈置了一個任務,做一個添加考試的app(在此之前一直漫無目地的學習ios,幾乎沒有進步,但做完這個程序感覺學到了好多,所謂在項目中成長嘛),在這裏總結一下我學到的東西:

1.首先大框架上是圍繞課程展開的,用面向對象來說課程就是一個類,展示課程要用到UITableView和cell,這便涉及到對象的封裝,cell的封裝,cell的重用,UITableViewDataSource和UITableViewDelegate協議方法,將對象放於數組內等等,之前做個幾個類似的程序,所以還不算困難。

2.點擊“添加考試”按鈕彈出添加界面用到模態窗口,這個也用過多次,還有就是要將數據傳回主界面創建cell,自定義協議,通過代理傳數據,被傳遞的控制器當做代理。

3.點擊cell進入一個詳情界面,顯示出課程各個內容,然後在詳情界面還有編輯按鈕,點擊進入編輯界面,這裏我沒有重新創建新的控制器,還是用創建界面那個控制器,但這會出現一個問題,創建課程和修改課程都需要向其他控制器傳遞數據,所以在編輯完或者創建完傳數據時需要判斷代理的類,如果代理是ViewController調用對應的代理方法,如果代理是DetailViewController調用對應代理方法。

4.時間選擇器UIDatePicker

有這麼幾種形式的時間UIDatePickerModeDate、UIDatePickerModeTime、UIDatePickerModeDateAndTime

#pragma 添加日期選擇器
- (void)addDatePicker
{
_datePicker = [[UIDatePicker alloc] init];
_datePicker.datePickerMode = UIDatePickerModeDateAndTime;
_datePicker.backgroundColor = [UIColor whiteColor];
[_datePicker addTarget:self action:@selector(chooseDate:) forControlEvents:UIControlEventValueChanged];
}

//設置日期格式
- (void)chooseDate:(UIDatePicker *)sender {
NSDate *selectedDate = sender.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd-HH";
NSString *dateString = [formatter stringFromDate:selectedDate];
_time.text = dateString;
}

//設置鍵盤和日期選擇器的彈出邏輯
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

//如果當前要顯示的鍵盤,那麼把UIDatePicker(如果在視圖中)隱藏
if (textField.tag != 1001) {
if (_datePicker.superview) {
[_datePicker removeFromSuperview];
} return YES;
}

//UIDatePicker以及在當前視圖上就不用再顯示了
if (_datePicker.superview == nil) {
//close all keyboard or data picker visible currently
[_name resignFirstResponder];
[_place resignFirstResponder];
[_other resignFirstResponder];

//此處將Y座標設在最底下,爲了一會動畫的展示
_datePicker.frame = CGRectMake(0, 480, 320, 216);
[self.view addSubview:_datePicker];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
_datePicker.frame = CGRectMake(0, 270, 320, 216);
[UIView commitAnimations];
}
return NO;
}


5.鍵盤遮擋TextField問題

<span style="font-size:14px;">//監聽鍵盤彈出
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification object:nil];
}

//鍵盤彈出時調用
- (void)keyboardWasShown:(NSNotification *)aNotification
{
    if (keyboardShown) return;
    if( _activeField == _other || _activeField == _place)
    {
        [_scrollView setContentOffset:CGPointMake(0.0, _activeField.frame.origin.y - 90) animated:YES];
        keyboardShown = YES;
    }
}

//鍵盤隱藏時調用
- (void)keyboardWasHidden:(NSNotification*)aNotification
{//獲取鍵盤size
 NSDictionary* info = [aNotification userInfo];
        NSValue* aValue = [info objectForKey:@"UIKeyboardBoundsUserInfoKey"];
        CGSize keyboardSize = [aValue CGRectValue].size;
    
    if (_activeField == _place) {

        [_scrollView setContentOffset:CGPointMake(0.0, _activeField.frame.origin.y-200) animated:YES];
        keyboardShown = NO;
    }
    if (_activeField == _other) {

        [_scrollView setContentOffset:CGPointMake(0.0, _activeField.frame.origin.y-keyboardSize.height) animated:YES];
        keyboardShown = NO;
    }
}

//跟蹤活動文本框的方法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    _activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _activeField = nil;
}

//點擊鍵盤return按鈕鍵盤隱藏
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (_activeField == _place) {
        
        [_scrollView setContentOffset:CGPointMake(0.0, _activeField.frame.origin.y-200) animated:YES];
        keyboardShown = NO;
    }
    if (_activeField == _other) {
        
        [_scrollView setContentOffset:CGPointMake(0.0, _activeField.frame.origin.y-253) animated:YES];
        keyboardShown = NO;
    }
    [textField resignFirstResponder];
    return YES;
}</span><span style="font-size: 18px;">
</span>

6.文件操作

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:name forKey:@"name"];
    [dic setObject:time forKey:@"time"];
    [dic setObject:place forKey:@"place"];
    [dic setObject:other forKey:@"other"];
    
//將bool類型轉化成NSNumber類型寫入字典
    NSNumber *aswitch = [NSNumber numberWithBool:isOn];
    [dic setObject:aswitch forKey:@"aSwitch"];

    [_dicCourse addObject:dic];
    
//獲取應用程序沙盒的Documents目錄
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    
//得到完整的文件名
    NSString *filename = [plistPath stringByAppendingPathComponent:@"Course.plist"];
//輸入寫入<pre name="code" class="objc" style="font-size: 18px;">//_dicCourse是一個裝有課程對象字典的數組
[_dicCourse writeToFile:filename atomically:YES];


從文件中讀出數據:

- (void)readFile
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    
//得到完整的文件名
    NSString *filename = [plistPath stringByAppendingPathComponent:@"Course.plist"];
//加載plist文件
    NSArray *array = [NSArray arrayWithContentsOfFile:filename];
//遍歷array數組
    for (NSDictionary *dict in array) {
        [_course addObject:[Course courseWithDict:dict]];
        [_dicCourse addObject:dict];
    }
}

7.長按刪除手勢

- (void)addGesture
{
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0; //seconds  設置響應時間
    lpgr.delegate = self;
    [_tableView addGestureRecognizer:lpgr];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{   //可以避免重複調用
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
<span style="white-space:pre">	</span>//判斷點擊cell的位置
        CGPoint p = [gestureRecognizer locationInView:_tableView ];
        NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:p];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"是否刪除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定 ", nil];
        [alertView show];
        index = indexPath.row;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [_course removeObjectAtIndex:index];
        
        [_dicCourse removeObjectAtIndex:index];
        
        //獲取應用程序沙盒的Documents目錄
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *plistPath = [paths objectAtIndex:0];
        //得到完整的文件名
        NSString *filename = [plistPath stringByAppendingPathComponent:@"Course.plist"];
        //輸入寫入
        [_dicCourse writeToFile:filename atomically:YES];
        [self.tableView reloadData];
    }
}


8.UIAlertView

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
此方法監聽點擊AlertView的按鈕,需要控制器遵守UIAlertViewDelegate協議,並作爲AlertView的代理

9.日期格式和計算

    NSDate *date = [[NSDate alloc] init];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-HH"];
    date = [dateFormatter dateFromString:cell.timeLabel.text];
    
    double time = [date timeIntervalSinceNow];
    int days = time/86400;
    int hours = time/3600 - days*24;
yyyy-MM-dd-HH爲日期格式,[date timeIntervalSinceNow]此方法返回從date到現在相差的秒數,下面再經過簡單的數學運算轉化爲天數和小時數。

小結:

早在寒假學長就鼓勵我們寫博客,說來實在慚愧,這期間一直沒有發現博客有這麼重要的作用,直到有一天閒來無事翻看龍澤學長的博客,發現與我與學長簡直天壤之別,感覺他的水平是我無法企及的。突然感到博客的重要性,一來總結自己的經驗,將來翻看要比直接項目方便的多;二來與網友分享自己遇到的問題的好的方法,共同進步;三來來記錄自己的學習經歷,將來萬一成了大神,也好講述自己學習的心路歷程,作爲炫耀的資本。(此條yy了一下0.0)。

第一次寫博客,沒有太多高深的見解,只是將學到的東西做個筆記,隨時翻看,加油!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章