2016-09-22 12 views
0

I 입력으로 stickerURLString를 수신 대리자 메서드 아래이 있습니다단추 매개 변수를 통해 다른 매개 변수에 문자열 매개 변수를 전달하는 방법은 무엇입니까?

- (void)selectedSticker:(NSString *)stickerURLString { 
    //... 
     [self.stickerPreviewButton addTarget:self action:@selector(sendStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    //... 
} 

그리고 그 선택이 방법 sendStickerPreviewButtonPressed 호출 :이 작업을하기 위해 볼 수 있듯이

- (void)sendStickerPreviewButtonPressed: (NSString *)stickerURLString { 
    [self.delegate InputFunctionView:self sendSticker:stickerURLString]; 
} 

을 예상대로 전달해야합니다 stickerURLString에서 selectedSticker 메서드를 sendStickerPreviewButton 프레스. 대신이의

[self.stickerPreviewButton performSelector:@selector(sendStickerPreviewButtonPressed:) withObject:stickerURLString]; 

:

나는 이것을 시도

[self.stickerPreviewButton addTarget:self action:@selector(sendStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

을하지만 오류 "유형 NSException의 캐치되지 않는 예외로 종료"를 받았습니다.

누구나 버튼 매개 변수를 통해 다른 매개 변수로 문자열 매개 변수를 전달하는 방법을 알고 있습니까? 당신은 당신이 변화를 사용할 수있는 함수를 호출 할 performSelector를 사용하는 경우

+0

가능한 복제 (http://stackoverflow.com/questions/24814646/attach-parameter-to-button-addtarget-action-in-swift) –

+2

전역 변수를 선언하고 메서드 내에서 값을 할당하십시오. –

+0

@AnuradhS 예, 코드가 복잡하기 때문에 전역 변수를 사용하지 않아도됩니다. 감사! – SanitLee

답변

0

UIControl 객체 함수 첫 번째 매개 변수는 id 유형이며, 컨트롤 자체를 전달하며, sendStickerPreviewButtonPressed의 매개 변수로 uibutton 객체를 가져옵니다. 문자열로 변환 할 수없는 함수입니다. 오류가 발생했습니다. 문자열을 전달할 수 있습니다. 사용자 정의 클래스로 UIbutton 클래스를 확장합니다. . 자신의 클래스의 버튼 만들기, anr 사용자 정의 속성 양식 발신자 단추를 얻을.

 -(void)sendStickerPreviewButtonPressed:(mycustomButton*)sender{ 
     //sender.myCustomString 
} 
[스위프트 button.addTarget의 작용을 매개 변수를 첨부]의