2012年8月14日火曜日

delegate

reblogメッセージはUIImageViewのサブクラスを作ってそいつが自らreblogのrequestを投げてresponseが返ってきたら自分の表示位置を示すシリアル番号(配列の添字)をdelegateに投げて表示位置をリセットして自分をフェードアウトしてremovefromsuperviewする形に変更。まだスマートかな。

rootのvc
- (void)rblg{
 int i;
 for(i=0;i<[rposArr count];i++){
  if([[rposArr objectAtIndex:i]isEqual:[NSNull null]]){
   [rposArr replaceObjectAtIndex:i withObject:@"used"];break;
  }
 }
 rblk=[[ReblogLike alloc]initWithFrame:CGRectMake(0,(sh-16)/2+i*16,sw,16) rorl:@"reblog" rid:[idArr objectAtIndex:now-1] rkey:[rblgkArr objectAtIndex:now-1] rpos:i
            num:now type:[typeArr objectAtIndex:now-1] cons:_consumer token:_accessToken
            url:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.tumblr.com/v2/blog/%@.tumblr.com/post/reblog",shtname]]];
 rblk.delegate=self;
 [self.view addSubview:rblk];
}
-(void)rposReset:(int)rpos{
 [rposArr replaceObjectAtIndex:rpos withObject:[NSNull null]];
}
ReblogLike.h
@protocol ReblogLikeDelegate 
-(void)rposReset:(int)rpos;
@end

@interface ReblogLike : UIImageView{
 int rpos;
 iddelegate;
}

@property(strong,nonatomic)iddelegate;

- (id)initWithFrame:(CGRect)frame rorl:(NSString *)rorl rid:(NSNumber *)rid rkey:(NSString *)rkey rpos:(int)rpos_ num:(int)num type:(NSString *)type
      cons:(OAConsumer *)cons token:(OAToken *)token url:(NSURL *)url;

@end
ReblogLike.m
- (id)initWithFrame:(CGRect)frame rorl:(NSString *)rorl rid:(NSNumber *)rid rkey:(NSString *)rkey rpos:(int)rpos_
    num:(int)num type:(NSString *)type cons:(OAConsumer *)cons token:(OAToken *)token url:(NSURL *)url
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
  sw=self.bounds.size.width,sh=self.bounds.size.height;
  rpos=rpos_;
  self.image=[UIImage imageNamed:@"img_black"];self.autoresizingMask=15;
  UIView *rBase=[[UIView alloc]initWithFrame:CGRectMake(0,0,sw,16)];rBase.autoresizingMask=13;
  title=[[UILabel alloc]init];
  title.text=[NSString stringWithFormat:[rorl isEqual:@"reblog"]?@"REBLOG-ing...":@"LIK-ing..."];
  title.font=sf(13);title.textColor=[UIColor whiteColor];title.backgroundColor=[UIColor clearColor];
  CGFloat titlewidth=[title.text sizeWithFont:title.font constrainedToSize:CGSizeMake(sw,15) lineBreakMode:UILineBreakModeTailTruncation].width;
  UILabel *info=[[UILabel alloc]init];
  info.text=[NSString stringWithFormat:@"(number %d - %@)",num,type];
  info.font=sf(10);info.textColor=[UIColor whiteColor];info.backgroundColor=[UIColor clearColor];
  CGFloat infowidth=[info.text sizeWithFont:info.font constrainedToSize:CGSizeMake(sw,12) lineBreakMode:UILineBreakModeTailTruncation].width;
  title.frame=CGRectMake(ceilf((sw-(titlewidth+infowidth))/2),0,titlewidth,15);
  info.frame=CGRectMake(ceilf(title.frame.origin.x+titlewidth+10),2,infowidth,12);
  [rBase addSubview:title];[rBase addSubview:info];
  [self addSubview:rBase];//return self;

  OAMutableURLRequest *request=[[OAMutableURLRequest alloc] initWithURL:url consumer:cons token:token realm:nil signatureProvider:nil];
  [request setHTTPMethod:@"POST"];
  NSMutableArray *prmt=[NSMutableArray arrayWithObject:[[OARequestParameter alloc]initWithName:@"id" value:[NSString stringWithFormat:@"%@",rid]]];
  [prmt addObject:[[OARequestParameter alloc]initWithName:@"reblog_key" value:rkey]];
  //[prmt addObject:[[OARequestParameter alloc]initWithName:@"comment" value:@"comment"]];
  [request setParameters:prmt];
  OADataFetcher *fetcher=[[OADataFetcher alloc] init];
  [fetcher fetchDataWithRequest:request delegate:self
     didFinishSelector:@selector(rblgTicket:didFinishWithData:)
       didFailSelector:@selector(rblgTicket:didFailWithError:)];

    }
    return self;
}
- (void)rblgTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data{
 NSDictionary *bd=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
 if([[bd valueForKeyPath:@"meta.status"]intValue]==201){
  title.text=@"REBLOG-ed.";
  [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveEaseIn
       animations:^{self.alpha=0;}
       completion:^(BOOL finished){
        [self.delegate rposReset:rpos];
        [self removeFromSuperview];
       }];
  //NSLog(@"rebloged.");
 }
 else NSLog(@"rblg failed.");
}
- (void)rblgTicket:(OAServiceTicket *)ticket didFailWithError:(NSError *)error{
 NSLog(@"rblg Error! %@",error);
}

できた。
delegateも難しく考えてたけど、単にずーっとやりたかったallocate元のインスタンスにメッセージを投げる方法だと考えればどーってことない。というか非常に使える。むしろ必須。わざわざnotification使ってたaccessTokenの取得ルーチンもdelegateに書き換えた。

0 件のコメント:

コメントを投稿