2015年10月3日 星期六

iOS筆記:UITextField & UITextView &Date Picker

UITextField

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    textField1 = [[UITextField alloc] init];
    textField1.frame = CGRectMake(10, 40, 200, 150);
    textField1.backgroundColor = [UIColor lightGrayColor];
    textField1.text = @"Test";

    textField1.font = [UIFont systemFontOfSize:20];
    textField1.textColor = [UIColor purpleColor];
    [self.view addSubview: textField1];

    textField1.textAlignment = NSTextAlignmentCenter; //設定對其方式
    textField1.autocorrectionType = UITextAutocorrectionTypeNo;//是不是要訂正輸入的字

    textField1.keyboardType = UIKeyboardAppearanceDefault;
}
    testField1.Delegate = self;
//文字欄位正在進行編輯時的動作
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

//鍵盤按下 Return ( Done ) 鍵時的動作
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //收起鍵盤
    [textField resignFirstResponder];
    return YES;
}

//TextField 文字欄位完成編輯時
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}

#pragma UIViewDelegate
//點擊文字框以外的地方時
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //指定欄位如在作用中,則隱藏鍵盤
    if( textField1.isTracking ){
        [textField1 resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

UITextView

使用上跟UITextField很相似

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    textView1 = [[UITextView alloc] init];
    textView1.frame = CGRectMake(40, 40, 200, 200);
    textView1.backgroundColor = [UIColor lightGrayColor];
    textView1.text = @"Default Test";

    textView1.font = [UIFont systemFontOfSize:20];
    textView1.textColor = [UIColor purpleColor];
    [self.view addSubview: textView1];

    textView1.textAlignment = NSTextAlignmentCenter; //設定對其方式
    textView1.autocorrectionType = UITextAutocorrectionTypeNo;//是不是要訂正輸入的字

    textView1.keyboardType = UIKeyboardAppearanceDefault;
    textView1.delegate = self;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView1 resignFirstResponder];
        return NO;
    }
    else {
    return YES;
    }
}

Date Picker

Apple認為選取器是實作日期跟時間選擇的最佳介面, 故提供兩種介面選擇, 一種為 Date Picker另一為自訂的Picker View.

要存取Date Picker存取的NSDate資料時就需要使用其date屬性. NSDateFormatter物件可以讓我們把NSDate物件轉換成字串. 如下面範例先初始化NSDateFormatter物件, 在使用setDateFormat方法, 建立出自訂格式. 再利用NSDateFormatter的另一個方法stringFromDate, 只要將NSDate傳入stringFromDate方法, 就會回傳指定格式.

enter image description here

測定時間差異
利用NSDate物件的timeIntervalSinceDate方法, 此方法會回傳兩個日期間的秒數差距.

- (IBAction)DateButton:(UIButton *)sender {
    NSDate *date = self.datePicker.date;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    #設定顯示的格式
    #[formatter setDateFormat:@"yyyy-MM-dd"];
    [formatter setDateFormat:@"yyyy/MM/dd/HH:mm"];

    NSString *stringFromDate = [formatter stringFromDate:date];
    NSLog(@"%@", stringFromDate);
    NSLog(@"%@", [NSDate date]); 
    #currently date.

    int timeInterval = [date timeIntervalSince1970];  
    #Give us the time interval or haw many seconds have passed since January 1,1970.

    NSLog(@"%i", timeInterval);
}

enter image description here

參考資料
1. http://unicode.org/reports/tr35/tr35-6.html#date_format_patterns
2. http://sh932111.pixnet.net/blog/post/29625583-ios_uitextfield-%E7%9A%84%E6%87%89%E7%94%A8-(%E7%B4%94%E4%BD%BF%E7%94%A8%E7%A8%8B%E5%BC%8F%E7%A2%BC%E8%A8%AD%E5%AE%9A)
3. http://rickyy-blog.logdown.com/tags/UITextField
4. http://www.cnblogs.com/ChinaKingKong/p/4690581.html

2015年10月1日 星期四

iOS筆記:PCH file

如果需要import的程式變多了之後,所有需要的檔案都必須要寫入import有時候變成一件煩人的事情。所有專案中的程式(.h 或 .m)都會預設去import你定義在pch檔案中的所#import的header,也就是如果你有很多檔案需要#import <MapKit/MapKit.h>,你可以只寫在這個import在Prefix.pch檔案中一次,在其它程式中就不用再次宣告了。

從XCode6之後, 新建專案默認是沒有pch文件的,如果我們想使用pch文件,需要手動添加.

  1. 首先新增一個PCH File,到該目錄底下。
    enter image description here

  2. 把.pch檔放置到Supporting Files目錄下

enter image description here

  1. 設定路徑

在Prefix Header 右側連點兩下並輸入檔案的路徑, precompile 也勾YES.

enter image description here

  1. 在.pch file實作
    ex.
    enter image description here

StackOverflow神人提供的幾個步驟:
a. Make new file: ⌘cmd+N
b. iOS/Mac > Other > PCH File > YourProject-Prefix.pch.
c. Project > Build Settings > Search: “Prefix Header”.
d. Under “Apple LLVM 6.0″ you will get the Prefix Header key.
e. Type in: “YourProjectName/YourProject-Prefix.pch”.
f. Clean project: ⌘cmd+⇧shift+K
g. Build project: ⌘cmd+B

參考:
1. https://www.youtube.com/watch?v=XfBWJEj7eoY
2. https://cg2010studio.wordpress.com/2014/10/31/xcode-%E5%89%8D%E7%B7%A8%E8%AD%AF%E6%A8%99%E9%A0%AD%E6%AA%94-pre-compile-header/
3. http://www.vincenttsai.com/category/iphone%E6%87%89%E7%94%A8%E7%A8%8B%E5%BC%8F%E9%96%8B%E7%99%BC/
4. http://www.vincenttsai.com/xcode-6-%E6%B6%88%E5%A4%B1%E7%9A%84-pch/