2011-07-01 2 views
1

"x", "y" "object"를 전달한 다음이를 이동시키는 일반적인 메서드를 작성하려고합니다.Button 개체를 메서드로 전달

-(void) changeObjectLocations: (integer_t) xSpot: (integer_t) ySpot: (id) sender { 

    if (![sender isKindOfClass:[UIButton class]]) 
    { 
     UIButton *myObject = (UIButton *)sender; 
     [UIView animateWithDuration:1.5    
     animations:^{     
     CGRect newFrame = myObject.frame; 
     newFrame.origin.x = xSpot; 
     newFrame.origin.y = ySpot; 
     myObject.frame = newFrame; 
    }];    
} 
    else if (![sender isKindOfClass:[UILabel class]]) 
    { 
    UILabel *myObject = (UILabel *)sender; 
    [UIView animateWithDuration:1.5 animations:^{     
     CGRect newFrame = myObject.frame; 
     newFrame.origin.x = xSpot; 
     newFrame.origin.y = ySpot; 
     myObject.frame = newFrame; 
    }]; 
    } 
} 

나는 다음과 같이 호출 할 : 현재 나는이 가지고

SecondViewController.m : 33

-(void) orientationBlockLandscape { 

    [self changeObjectLocations: 456 :282 : btn1] ; 
    [self changeObjectLocations: 391 :227 : lblTitle] ; 

} 

가 작동하고 있지만, 컴파일에 나는 다음과 같은 경고를 얻을 수 : 경고 : 'SecondViewController'가 '-changeObjectLocations :::'에 응답하지 않을 수 있습니다.

개체를 전달할 수 있거나 전달해야하는 더 나은 방법이 있습니까? 모든 도움에 미리 감사드립니다. 당신이 당신의 SecondViewController의 헤더에 changeObjectLocations를 정의하지 않았다처럼

지오 ...

출력 경고를 기반으로

답변

1

, 그것은 소리 - 아니면 이미 구현 한 것과 동일한 서명이 아니다.

+0

그게 전부입니다. 목표 -C를 처음 접했을 때 나는 항상 무언가를 잊어 버렸습니다! 감사합니다 – George

+0

@ 문제 없습니다. 그런데 위대한 이름! ;) –

0

컴파일러는 변수가있는 메서드 정의 인 선택기를 찾고 제거합니다.

그래서 같은 방법

-(void) setObject:(id) anObject forKey:(NSString *) keyname; 

이 ...의 선택 것이다 : 따라서

setObject:forKey: 

을, 당신의 방법

-(void) changeObjectLocations: (integer_t) xSpot: (integer_t) ySpot: (id) sender 

는 ...해야합니다 선택자 :

changeObjectLocations:xSpot:ySpot: 
파라미터 이름이 너무 셀렉터의 일부인 것을

참고

changeObjectLocations:xSpot:ySpot: 

changeObjectLocations::: 

은 .. 두 완전히 분리 방법을 나타내는 두 개의 전혀 다른 선택기이다.

예를 들어, 이름없이 매개 변수를 사용하는 것은 언어에서 합법적이지만. ":::"명명 규칙 충돌이 발생하기 쉽기 때문에 매우 빈약 한 방법입니다. 명시 적으로 작성하면 코드를 읽고 유지하기가 쉬워 질뿐만 아니라 컴파일러와 런타임이 더 쉽게 작동 할 수 있습니다.

관련 문제