plistのパスの変更

plistを新しいディレクトリなどに移して、パスが変わってしまった時の対処法。
The file “Info.plist” couldn’t be opened because there is no such file.というエラーが出た時

f:id:ichi6161:20170420152726p:plain

target→Packagingの中にあるinfo.plist File項目のパスを変更しなければならない。

f:id:ichi6161:20170420152940p:plain

今回はplistをOthersディレクトリに入れて管理しているのでpassをCalendar/Others/Info.plistと変更したらOK

AutoLayout1

AutoLayoutのポイント

・Constraintsを設定する前に、だいたいの大きさや位置はpreviewで設定しておく。そうしないと、Constraints変になる

①全画面にimageViewを表示する

UIImageViewを選択した状態で、add new constraintsを選択する。

Marginを上下左右0に設定。Add 4 Constrainsを押すのを忘れずに。

f:id:ichi6161:20170418155810p:plain

黄色いボタンwarning button が出たら、previewとconstraintsがずれているという意味。
f:id:ichi6161:20170418155804p:plain

黄色い部分をクリック。Fix Missplacemantをクリックする
f:id:ichi6161:20170418155759p:plain


②ボタンやラベルを高さ50で3つ重ねる場合

それぞれのボタンで同じように設定すれば0K f:id:ichi6161:20170420004359p:plain


③ボタンの高さと幅を同じ割合で配置

f:id:ichi6161:20170420005259p:plain

Aspect Ratioにチェックを入れる

f:id:ichi6161:20170420005307p:plain

Multiplierの所でwidth:heightで設定できる

f:id:ichi6161:20170420005221p:plain


④中央に以下のように、画面中央に、画面幅の60%の幅で、ボタンを配置する

f:id:ichi6161:20170420010301p:plain

まずは中央に配置
f:id:ichi6161:20170420011034p:plain

buttonを押した状態で、controlを押しながら、viewと結びつけ、まずはEqual Widthsにチェック
f:id:ichi6161:20170420011137p:plain

Multiplierで0.6に設定
f:id:ichi6161:20170420011350p:plain

図だと一応Aspect Ratioも設定している

f:id:ichi6161:20170420011657p:plain


⑤複数のボタンを上から100ピクセルの位置に、同じ幅で3つ配置する。ボタンの高さは50ピクセル

f:id:ichi6161:20170420141341p:plain

まず基準となるbuttonのConstraintsを設定
f:id:ichi6161:20170420141359p:plain

2番目のbuttonの設定

f:id:ichi6161:20170420141441p:plain

最後のbuttonの設置
f:id:ichi6161:20170420141625p:plain

赤、緑、黄色の順でクリックし高さと幅を基準(黄色)に合わせる
f:id:ichi6161:20170420141724p:plain

最後に3つのbuttonの中心が基準と同じになるように設定
f:id:ichi6161:20170420141844p:plain

デバイスのディレクトリパスとNSFileManager

データをどのディレクトリに入れるかを整理する。

・cachesディレクト

このディレクトリ下のデータは、アプリがアクティブの時でも削除される可能性がある

・tmpディレクト

このディレクトリ下のデータは、 アプリがアクティブの時は保持され続ける

・Documentsディレクト

このディレクトリ下のデータは、アプリがアンインストールされない限り保持され続ける

参考 [http://qiita.com/zck/items/9d087d498ce711b22c04:title]

tmpディレクトリまでのパス

NSString *tmpDirPath = NSTemporaryDirectory();

cachesディレクトリまでのパス

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *DocumentsDirPath = [paths objectAtIndex:0];

Documentsディレクトリまでのパス

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirPath = [paths objectAtIndex:0];

ファイルを保存したり、管理する場合、通常これらどのディレクトリにしても配下に新しいディレクトリを用意して管理することになる。
そこでNSFileManagerを使用して、新しいディレクトリを作り、imageファイルを保存する方法をおさらい

今回は、Documentsディレクトリ配下にsampleDirectoryというディレクトリを作る。

viewdidloardとかに
Asset catalogで指定した画像をimageに入れる(Asset catalogに入れた場合、画像は消去してok)

UIImage *image = [UIImage imageNamed: @"dog"];
    NSData *imageData = UIImagePNGRepresentation(image);


ディレクトリ作成の前に新しく作るディレクトリまでのpathを取得する。

NSString *newDocumentsDirPath = [DocumentsDirPath stringByAppendingPathComponent:@"sampleDirectory"];


この時点ではまだpathを取得しただけ。実際にディレクトリは存在しない

NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;

//ディレクトリを作る
    BOOL created = [fileManager createDirectoryAtPath:newDocumentsDirPath withIntermediateDirectories:YES attributes:nil error:&error];
    
    if(!created){
    
        NSLog(@"作れなかったよ");
    }


stringByAppendingPathComponentメソッドを使用して、ディレクトリ配下にdog.igoファイルを追加したpathを取得しwriteToFileメソッドでファイルを書き込む

NSString *filePath =  [newDocumentsDirPath stringByAppendingPathComponent:@"dog.igo"];

[imageData writeToFile:filePath atomically:YES];

 ```

自作アプリのUrlスキーマを設定

自作アプリをサファリなどの他のアプリから開く

プロジェクトのターゲットをクリック URL Types で設定 identifier と URL Schemes を設定。Identifierは Bundle Identifierと同じで良い

f:id:ichi6161:20170416225506p:plain

plistには自動で値が入る f:id:ichi6161:20170416225512p:plain

開くときは、設定したUrl Scheme://で開く。
例 stvichi://

プロジェクトに新しいディレクトリを追加

新しいディレクトリを追加するとき

① 実際のファイルシステム上でデレクトリを作る

② xcode上で新しいグループを作る。

③ xcode右側 file inspector でlocationの下、ディレクトリマークをクリックして、作成したグループとファイルシステム上のディレクトリを結びつける

④ ファイルを新規で作成する場合は、クラス名をつけた後、ディレクトリを指定することを忘れない。

⑤ 新しく作ったディレクトリに入れたいファイルがすでに作成済みの場合。まずファイルシステム上でファイルをディレクトリに移動

⑥ xcode上でファイル名が赤く表示されるのでデリートする。

f:id:ichi6161:20170416173949p:plain

ファイルシステムからドラッグアンドドロップxcodeのproject navigator に追加。

日付の処理+カレンダーを作る時のポイント

日付処理の検証

年、月、日付をそれぞれ別にして取得したい場合  
  - (NSDateComponents )components:(NSCalendarUnit)unitFlags fromDate:(NSDate )date;メソッドを使う

解説
- (NSDateComponents *)components:(NSCalendarUnit)年 | 月 | 日 | fromDate:年月日を取り出したい日付;

//今日の日付を取得したい場合
NSDate *today = [NSDate date];</br> 
###日付の処理

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today];
    

    NSLog(@"today : %@",today);

    NSLog(@"day   :%ld",components.day);

    NSLog(@"month :%ld",components.month);

    NSLog(@"year  :%ld: ",components.year);

結果

2017-04-09 20:54:59.683 myCalendar[40300:2824145] today : 2017-04-09 11:54:59 +0000
2017-04-09 20:54:59.684 myCalendar[40300:2824145] day   :9
2017-04-09 20:54:59.684 myCalendar[40300:2824145] month :4
2017-04-09 20:54:59.684 myCalendar[40300:2824145] year  :2017: 

上記作成したcomponentsの年月日をしてしてみる

components.day = 1;

NSDate *whichDateOfMonth = [[NSCalendar currentCalendar] dateFromComponents:components];
    NSLog(@"%@",whichDateOfMonth);

結果

2017-04-09 20:54:59.683 myCalendar[40300:2824145] 2017-03-31 15:00:00 +0000

そして次の場合

components.day = 20;
components.month = 5;

NSDate *whichDateOfMonth = [[NSCalendar currentCalendar] dateFromComponents:components];
    NSLog(@"%@",whichDateOfMonth);

結果

2017-04-09 20:54:59.683 myCalendar[40300:2824145] 2017-05-19 15:00:00 +0000

NSDate *whichDateOfMonth = [[NSCalendar currentCalendar] dateFromComponents:components]; NSLog(@“%@”,whichDateOfMonth);


その月の日数を取得する 

NSInteger numberOfWeeks = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitWeekOfDay //日 inUnit:NSCalendarUnitMonth//月の forDate:firstDateOfMonth //この日を含む].length //数;

メソッドの訳(この日を含む月の中の日数をNSIntegerで取り出して)



[カレンダーを作る]()にアクセスしてますか?
###カレンダーを作る時
1.今日の日付を取得</br>
2.今日の日付を年、月、日というように要素ごとに取り出す
3.今月が何日から始まるかを取得(普通に一日)</br>


2. 上記 - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date;メソッドを使う</br></br>

解説</br>
- (NSDateComponents *)components:(NSCalendarUnit)年 | 月 | 日 | fromDate:年月日を取り出したい日付;

[[NSCalendar currentCalendar] dateFromComponents:];

CollectionViewのカスタムセル

まず、controllerViewのクラス名変更したらストーリーボードも変更することを忘れない

f:id:ichi6161:20170409182049p:plain

collectionViewをはったらヘッダーファイルと紐付け
f:id:ichi6161:20170409182328p:plain

xibファイルもセットで新しいUICollectionViewCellクラスのファイルを作る f:id:ichi6161:20170409182835p:plain

トーリボードでクラス名とセルのIdentiferを設定する f:id:ichi6161:20170409182336p:plainf:id:ichi6161:20170409182341p:plain

xibファイルはこんな感じでラベルをつけてDayCell.hに紐付けしておく f:id:ichi6161:20170409183600p:plain

コードの実装

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    rows = @[@"あ",@"い",@"う"];
    
    //xibファイルのセルを(UICollectionCellクラスのファイル生成時に作ったのでxibファイルはUICollectionCellと元々結びついている)ストーリボードのセルと結びつける
    UINib *nib = [UINib nibWithNibName:@"DayCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"Cell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return rows.count;;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

//セルのインスタンス生成しストーリーボードのセルと結びつける
 DayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
//    DayCell *cell = [collectionView dequeueReusableCellWithIdentifier:cellName
//                                                       forIndexPath:indexPath];
    
    // カスタムセルのラベルに値を設定
    cell.dayLabel.text = rows[indexPath.row];
    
    return cell;
    
    /* 標準Cellを使用する場合
     NSString *cellName = NSStringFromClass([UICollectionViewCell class]);
     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellName forIndexPath:indexPath];
     return cell;
     */
}

このように、①xibとストーリーボード結びつけ、viewControllerで生成したUICollectionViewのインスタンスとストーリーボードのセルの結びつけという2段階。

結果

f:id:ichi6161:20170409184659p:plain

参考: tableViewの場合

Objective-C:カスタムセルを使ったUITableView - asky