Sunday, July 28, 2013

UITextFieldを縦方向に真ん中&TableViewのgroupedでUITextFieldを表示&UINavigationBarにボタン追加

UITextFieldを縦方向に真ん中にするのは以下のコード

    UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 200, 100)];
    field.text = @"sample";
    field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:field];

高さをUITableViewCellの高さに合わせればOK

-----------------------------------------------------

UINavigationbarの右にボタンを加える。storyboardではできないからね


    UIBarButtonItem *send = [[UIBarButtonItem alloc]initWithTitle:@"保存"
                                                            style:UIBarButtonItemStyleDone
                                                           target:self
                                                           action:@selector(saveSetting:)];
    self.navigationItem.rightBarButtonItem = send;


こんな感じかなー

keyboard observer

keyboardが出てきたときに呼び出されるメソッドを作る
作り方はNSObserverという物を使う


-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
    // observerを取り除く
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardWillShow:(NSNotification *)aNotification {
    // keyboardが現れたときの処理
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    // したにあったテキストボックスなどを見えるところまで表示。
    // キーボードの大きさを取得する
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // prekeyboardHeightはすでにクラス全体で使えるように定義しておく。
    if (prekeyboardHeight != keyboardRect.size.height) {
        float change = keyboardRect.size.height - prekeyboardHeight;
        prekeyboardHeight = keyboardRect.size.height;
        keyboardRect.size.height = change;
    }
    
    // textFieldを押し上げる。
    CGRect textRect = self.text.frame;    
    textRect.origin.y = textRect.origin.y - keyboardRect.size.height;
    self.text.frame = textRect;

    NSLog(@"keyboard appear");
}
-(void)keyboardWillHide:(NSNotification *)aNotification {
    // keyboardが消えたときの処理

    // 今度はキーボードが消えたときに位置を調整する
    // キーボードの大きさを取得
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if (prekeyboardHeight != keyboardRect.size.height) {
        float change = keyboardRect.size.height - prekeyboardHeight;
        prekeyboardHeight = keyboardRect.size.height;
        keyboardRect.size.height = change;
    }
    
    // 全体を押し下げる。
    CGRect textRect = self.text.frame;
    textRect.origin.y = textRect.origin.y + keyboardRect.size.height;
    self.text.frame = textRect;

    prekeyboardHeight = 0;

    NSLog(@"keyboard disappear");
}


なんでviewDidLoadじゃなくて、viewWillAppearに書いてあるかというと、viewDidLoadに書いたらなんかうまくいかなかったから。 多分、NSNotificationCenterはインスタンスが作られていないとだめみたいな決まりがあるのだろう

Tuesday, July 23, 2013

いろいろ

いろいろなtips

cssで図形を作る

phpでカレンダーを作る

';
    $counter++;
}
for ($day = 1; $day <= $last_day; $day++) {
    echo '' . $day . '';

    $counter ++;
    if ($counter == 7) {
        $counter = 0;
        echo '';
    }
}

?>


多分こんな感じで作れる

Sunday, July 21, 2013

コードで作ったUIViewをself.○○で使えるようにする

プログラムでUIViewパーツを作ったけどほかの場所からのアクセスの仕方がわからないって時


// .hファイル
    @property (weak, nonatomic) IBOutlet UILabel *text;

// .mファイル
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(100, 100, 50, 20);
    label.text = @"sample Label";
    [self.view addSubview:label];
    self.text = label;


こうすれ楽勝だ

UIToolBarに3セクションのUISegementedControlを加える

最近あまり開発の時間が取れていないkoheiです。

今日はtoolbarに3セクションのUISegementedControlを加える方法です。
早い話がこれのことです。

xcodeのstoryboardでUISegementedControlをtoolbarに持っていくと、勝手に黒くちいさくしてくれるのですが、セグメントの数は変えられません。
なので、プログラムで書いていくことになります。

そして、こちらがソースコードです。


    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolbar.barStyle = UIBarStyleBlackOpaque;
    
    UISegmentedControl *segs = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"一つ目", @"二つ目", @"3つ目", nil]];
    [segs setSelectedSegmentIndex:0];
    [segs setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segs addTarget:self action:@selector(optionChanged:) forControlEvents:UIControlEventValueChanged];
    
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:segs];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    [toolbar setItems:[NSArray arrayWithObjects:space, buttonItem, space, nil]];
    [self.view addSubview:toolbar];


簡単ですね♪
こちらをコピペしました→http://www.18th-technote.com/post/9718092082/uitoolbar

xcodeでのバリデーション

メールアドレス
- (BOOL) validateEmail: (NSString *) candidate {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    return [emailTest evaluateWithObject:candidate];
}

これでOK

参照→http://sugartin.info/2011/08/17/regular-expression-for-email-validation-in-xcode-iphone-application/

パスワード

まだわからない。これから調べる