なんとなく

忘備録です

アイテム名とか知りたい時のメモ

iアプリを普段扱ってると、あー コレTableViewかー!とか、このオブジェクト何?的な事を
思ったりする。(開発してるから?私だけ?)
こないだも、あるアプリで 何これ??? どうやって作ってるの?と思って google先生
聞いてみるけど、聞き方が悪いのか、ちっとも引っかからない。
会社の人に聞いたら、あー!って検索して、教えてくれた。結局 オブジェクト名では
なかったけど、コレいいなー なんて思う時は、大抵 海外のモバイルデザインUIサイトを見てます。

Appleユーザーインターフェースガイドラインも必要だろうけど、正直、読むの辛い。
(いつか読む…つもり…w)

てことで、UIの参考になるサイトをメモしとこう。

☆Mobile Design Pattern Gallery:
http://www.mobiledesignpatterngallery.com/mobile-patterns.php

☆pttrns 926
http://pttrns.com/


ま、覚書なので。

@property メモ

ヘッダーファイル(.h)等で見る「@property」。
インターフェースビルダー(.xib)からヘッダーファイルに直接結びつける
(オブジェクト選択して、Ctrl押したままヘッダーファイル定義)と、
特に何も考えなくて良い(というか考えたこと無い…orz)

プロパティの中身については、下記参照。

[iOS]@propertyのあれこれ
http://cheesememo.blog39.fc2.com/blog-entry-84.html

なるほど。変数やオブジェクトの状態設定な訳ね。


[iOS]@propertyのあれこれ2
http://cheesememo.blog39.fc2.com/blog-entry-184.html

どうやら、assign が、保持するってことね。

で?

Java でいう static とかそういうものでも無さそう…な気がする。

ちゃんと 分かっとかないと、後で痛い目に合うな。
痛い目に合わないと、分からないかも という噂も…

キーボードに閉じるボタンをつけるメモ

文字列入力するときのキーボードに、閉じるボタンとか付いてるのはよく見るけど、
設定?と思ってました。が、どうやら、キーボード(というか、UITextField)のAccessoryViewに
インスタンスしたViewをセットしてあげれば良いらしい。

一昨日のUITableViewに追加してみた。

で、TextFieldの設定のところで、追加する。
LoginViewController.m

// テーブルの列にデータセット
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITextField *passTextFld;
        :インスタンス&設定
    // テキスト
    passTextFld = [[UITextField alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 140.0f, 50.0f)];
    passTextFld.delegate = self;
        :設定
    passTextFld.inputAccessoryView = [self getAccessoryView];
    
    if (indexPath.row == 0)
    {
        [nameLabel setText:@"ユーザーID"];
        :
    }
    else
    {
        [nameLabel setText:@"パスワード"];
        :
    }
}

AccessoryView生成&取得

- (UIView *)getAccessoryView
{
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 40.0f)];
    accessoryView.backgroundColor = [UIColor blueColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(250.0f, 5.0f, 65.0f, 34.0f);
    [button setTitle:@"とじる" forState:UIControlStateNormal];
    
    // ボタンを押した時のイベント
    [button addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside];
    
    // View にボタン追加
    [accessoryView addSubview:button];
    
    return accessoryView;
}

結果。

ハイ。できました♪

が、しかし、、、コレはなぜ???

同じテキストフィールドにセットしてるのに。orz
UITableViewのIndexPathが違うだけデス。先頭のユーザーID(UITableViewのRowIndex=0)はダメで、
パスワードはOK。cellForIndexPath…双方共設定してるつもりなのに…

先は長い。


【参考サイト】
☆キーボードを閉じるためのボタンを追加する
http://works.sabitori.com/2011/06/18/add-button-to-keyboard/
☆Customizing the iOS keyboard
http://blog.carbonfive.com/2012/03/12/customizing-the-ios-keyboard/

UITableViewでUITextFieldのカーソル移動を行うメモ

単に、UITextFiledだけのオブジェクトを使うんなら、カーソル移動(入力開始)は

[hogeTextField becomeFirstResponder];

だけで、オッケー。
なんだけど、テーブルを使って、UITableViewCellにセットされたUITextFieldにカーソル移動しようとして
そもそも、どうやってTableセット済のCellの中身を取るのか分からないし…
なので、メモ。

動作は、ログイン画面用のUITableView(0:ユーザーID、1:パスワード)で、ユーザーIDのUITextFieldでReturn→
パスワードTextFieldにカーソル移動。

1) テーブルセルの設定
ViewController.h

// UITextFieldタグ
#define TAG_USER_ID 1
#define TAG_PASSWORD 2
// UITableView-RowIndex
#define ROW_IDX_USERID 0
#define ROW_IDX_PASSWORD 1

// テーブルの列にデータセット
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *nameLabel;
    UITextField *inputTextFld;
    UIFont *textFont;
   // CGRect *cellRect;
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]
                initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }
    // ラベル
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 130.0f, 50.0f)];
    [cell.contentView addSubview:nameLabel];
    
    // テキスト
    inputTextFld = [[UITextField alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 140.0f, 50.0f)];
    inputTextFld.delegate = self;
    
    if (indexPath.row == 0)
    {
        [nameLabel setText:@"ユーザーID"];
        inputTextFld.placeholder = @"ユーザーID";
        inputTextFld.returnKeyType = UIReturnKeyNext;
        // UITextField取得用タグ
        inputTextFld.tag = TAG_USER_ID;
    }
    else
    {
        [nameLabel setText:@"パスワード"];
        inputTextFld.placeholder = @"パスワード";
        inputTextFld.secureTextEntry = YES;
        inputTextFld.tag = TAG_PASSWORD;
    }
    [cell.contentView addSubview:inputTextFld];
    
    return cell;
}

2) テキストフィールドでReturnした時

// TextField Returnタップ
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == TAG_USER_ID)
    {
        // TableのIndexPathリストを取得
        NSArray *indexPathArr = loginInputTbl.indexPathsForVisibleRows;
        // パスワードセルのIndexPathを取得
        NSIndexPath *indexPathPassword = [indexPathArr objectAtIndex:ROW_IDX_PASSWORD];
        // パスワードのセルを取得
        UITableViewCell *cellPass = [loginInputTbl cellForRowAtIndexPath:indexPathPassword];
        UITextField *passText = (UITextField*)[cellPass viewWithTag:TAG_PASSWORD];
        // パスワードの入力開始(カーソルセット)
        [passText becomeFirstResponder];
    }
    else
    {
        // キーボードを閉じる
        [textField resignFirstResponder];
    }
    
    return YES;
}

コレのキモは、設定済のUITableViewCellを取得すること。テーブルを選択してない時に、
設定済のIndexPathを取るのが、最初の問題でしたw

【参考サイト】
☆indexPathからUITableViewCellを取得するには?
http://cheesememo.blog39.fc2.com/blog-entry-291.html

☆UITableViewにUITextFieldを入れる
http://shiffon.dtiblog.com/blog-entry-40.html

☆login using UITableView
http://stackoverflow.com/questions/5063061/login-using-uitableview

UITableViewでUITextFieldを使うメモ

よくあるログイン画面は、UITableView で作ってある(気がする)。
なので、それでやりたい。で、まず一歩目が、UITextFieldの配置。簡単そうだけど、意外とハマった。

やりたいのは、こんなの。UITableViewCell で UITextField を使う。入力フィールドがCellの真ん中に表示されるようにする。
   

TableViewCell の設定。
LoginViewController.m

#define TAG_USER_ID 1
#define TAG_PASSWORD 2

// テーブルの列にデータセット
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *nameLabel;
    UITextField *passTextFld;
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIFont *textFont = [UIFont systemFontOfSize:17.0];
        
        // ラベル
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 130, 50)];
        nameLabel.backgroundColor = [UIColor clearColor];
        [nameLabel setFont:textFont];
        [cell.contentView addSubview:nameLabel];
        
        // テキスト
        passTextFld = [[UITextField alloc] initWithFrame:CGRectMake(130, 20, 140, 50)];
        passTextFld.delegate = self;
        [passTextFld setFont:textFont];
        passTextFld.autocapitalizationType = UITextAutocapitalizationTypeNone;
        passTextFld.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        if (indexPath.row == 0)
        {
            [nameLabel setText:@"ユーザーID"];
            passTextFld.placeholder = @"ユーザーID";
            passTextFld.returnKeyType = UIReturnKeyNext;
            passTextFld.secureTextEntry = NO;
            passTextFld.tag = TAG_USER_ID;
        }
        else
        {
            [nameLabel setText:@"パスワード"];
            passTextFld.placeholder = @"パスワード";
            passTextFld.returnKeyType = UIReturnKeyDefault;
            passTextFld.secureTextEntry = YES;
            passTextFld.tag = TAG_PASSWORD;
        }
        [cell.contentView addSubview:passTextFld];
    }
    
    return cell;
}

高さとかはこんな感じ。

// セクション数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//セクションのタイトルを設定
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return nil;
}

// テーブルセルの高さを設定
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

// tableのリスト件数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

#pragma mark - TextField Check

// TextField Returnタップ
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // キーボード非表示
    [textField resignFirstResponder];
    
    return YES;
}

ヘッダーファイルにUITextFieldの操作を行うDelegateメソッドを定義しとく。
LoginViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface LoginViewController : UIViewController <UITextFieldDelegate>
{
}

結果は…
   

なんか変。orz
ラベル(ユーザーID)と入力テキストの配置、高さを同じにしてるのに、なんかズレる。

frameのCGRect指定で、雰囲気、設定してみた。→ダメ。
なら、frameの横位置で中央に設定するのは、幅/2 ってよくある。なら、高さも半分じゃ?てことで、
やってみた。→ダメ。orz そらそうよね、高さ50/2 でY軸の位置を指定したって…w

で、ふと思い出した。
InterfaceBuilder で、UITextField の設定を見ると、なんか、真ん中に表示する ってあったよなー と。
試しに見てみた。

UITextField

てことで、正解src☆

// テキスト
passTextFld = [[UITextField alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 140.0f, 50.0f)];
passTextFld.delegate = self;
[passTextFld setFont:textFont];
passTextFld.keyboardType = UIKeyboardTypeASCIICapable;
// 高さ:Center
passTextFld.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
passTextFld.autocapitalizationType = UITextAutocapitalizationTypeNone;

時間かかったけど、出来たので、オッケー♪

【参考サイト】
☆座標の指定が整数値でない場合 UIKit の描画(ビュー、画像、ボタン、その他いろいろ)がぼやける
http://d.hatena.ne.jp/KishikawaKatsumi/20100527/1274910461
☆UITextField horizontal and vertical alignment
http://cocoamatic.blogspot.jp/2011/01/uitextfield-horizontal-and-vertical.html