2014年3月19日水曜日

ios 同期通信でpostする

iosで同期通信でpostします。

いきなりcodeです。

     
    // Send a synchronous request
    NSURL *url = [NSURL URLWithString:@"url名"];
    // post情報をくっつけるので、NSMutableURLRequestを使用
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // Set the method(HTTP-POST)
    [request setHTTPMethod:@"POST"];
    // Set the request-body.
    NSString *reqBody = [NSString stringWithFormat:@"login[account]=%@&login[password]=%@",account名,password];
    // utf8にする
    [request setHTTPBody:[reqBody dataUsingEncoding:NSUTF8StringEncoding]];
    
    // 吐き出すエラー
    NSError * error = nil;
    NSURLResponse* response = nil;
    
    
    
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                          returningResponse:&response
                                                      error:&error];
    // 通信が行なわれた後に実行されます。
   // 電波が悪く通信できなかった(errorStringにinternet can't useとかデル)
    NSString *errorString = [error localizedDescription];
    if(0 < [errorString length])
    {
       // error処理
    }
    // 通信成功
    else
    {
       // utf8で返った情報をとってくる
       NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
       // jsonデーターをとってこよう
       NSDictionary* jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
       // 返った情報を表示
       NSLog(@"result: %@", result);
    }


解説は、コメントに書いたので、特にないっすね。

2014年3月18日火曜日

ios Bundle Identifierの初期値を設定する方法

ios Bundle Identifierの初期値を設定する方法です。

githubから参考したいプロジェクトをダウンロードした時に、
bundle identifierに元で設定してあった値が代入されてしまっていたので、
それを直したいと思って調べました。


まず、プロジェクトのplistファイルをクリックします。


すると、次の画面が出てきます。


Bundle identifierを変えてください。

2014年3月3日月曜日

ios xibで作ったカスタムセルをTableViewに登録する方法

ios xibで作ったカスタムセルをTableViewに登録する方法

まずは、対象となるUITableViewにxib部品を登録します。


    UINib *nib = [UINib nibWithNibName:@"ButtonCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"PagingCell"];
登録するxibファイル名をnibWithNibNameに登録し、 xibで作成した、セル名を登録します。
そして、呼び出す時は、次のように記述します。

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    PagingCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"PagingCell"];
    // セル特有の処理

    return cell;
}



cxc

2014年2月26日水曜日

ios UITableViewで1行のみを更新する方法

ios UITableViewで1行のみを更新する方法です。

更新したい場面で、
    
[self.tableView reloadRowsAtIndexPaths:
[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];

を記述します。


この場面では、1Section,0Rowのテーブルセルを更新しています。

0Sectionの0Rowから、0Sectionの8Rowまでを更新したい場合はforを使ってやればOK

ios 親から子への値渡し

ios 親から子への値渡しのやり方です。


StoryBoardを使用して、ナビゲーションコントローラーの親から子へデリゲートを使用して、値渡しを行いたいと思います。

まず、StoryBoardの設定からです。



ナビゲーションコントローラーを引っ張って、2つのビューコントローラーに結びつけます。


1番目のViewControllerにBar Button Itemを置いて、次のViewControllerにセグウェーでひもづけます。

やり方は、

1.Bar Button ItemのSecondを右クリック

2.そのまま移動すると、青い線が引かれると思うので、SecondViewControllerにもっていって、そのままドラックを解放します。

3.すると、push,modalなどを選択する表示画面が現れるとおもうので、
pushを選択します。

これで、画面遷移が完成しました。


○次は、値渡しです。

親をViewController,子をViewController2とします。


まずヘッダーに記述します。(ViewController.h)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong,nonatomic) NSString* str;

@end

続いて子のヘッダーにも同じプロパティを用意します。

(ViewController2.h)

#import <UIKit/UIKit.h>


@interface ViewController2 : UIViewController
@property (strong,nonatomic) NSString* myStr;
@property (weak, nonatomic) IBOutlet UILabel *label;  // 値表示用

@end


ヘッダーにプロパティを代入しました。
では、実際に遷移するところを見ます。
今回は、StoryBoard上で、BarButtonItemからセグウェーを仕込んだので、
画面遷移前に呼び出される
prepareForSegueメソッドで値渡しのプログラムを記述します。

StoryBoard上から遷移したので、遷移先のidentifierを登録して
プログラム上で、どこへ遷移するのか判別できるようにします。


遷移先のidentifierに入力したら画面遷移のところで、値を渡せるようにします。

(ViewController.m)

/*
    画面遷移前に呼び出されるメソッド
*/
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    _str = @"delegate";
    if ( [[segue identifier] isEqualToString:@"SecondPush"] ) {
        ViewController2 *Con2 = [segue destinationViewController];
        Con2.myStr = str;   // 子に親の値を渡す
    }
}

これで、子に値を渡すことができました。


2014年2月24日月曜日

UITableViewのセルをハイライトさせない方法(StoryBoard)

UITableViewのセルをハイライトさせない方法(StoryBoard)です。

StoryBoardからハイライトさせない場合は、
テーブルのセルを選択して、Attribute InspectorのSelectionをNoneにします。


コードでやる場合


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

メソッドで、生成されたセルに対して、
cell.selectionStyle = UITableViewCellSelectionStyleNone;

を入れてやる。

jsonで返ってくるnullをobjective-cで判定する

jsonで返ってくるnullをobjective-cで判定する方法です。

以下のようなjsonデーターがあるとします。

hoge{

    hoge2:[
        {
        },
        {
        }
    ],
}

hoge2の中身が空かどうか判定したい場合


if([jsonObj valueForKeyPath:@"hoge.hoge2"] = null)
{

}

(jsonObj はNSDictionary型のjsonを取得したオブジェクト)

では、nullを判定してくれません。


では、どうするかというと...


if([jsonObj valueForKeyPath:@"hoge.hoge2"] = [NSNull null])
{

}

これで、nullを判定してくれます。