2016-10-13 7 views
1

내 코드에서 targetMethod printMethod에 두 개의 인수를 전달해야합니다. 하나의 인수로 button.tag를 전달할 수 있으며 다른 인수를 전달하는 방법은 무엇입니까?iOS에서 UIButtons 대상 메서드에 두 개의 인수를 전달하는 방법은 무엇입니까?

예제를 제공해주십시오.

내 코드 :

button.tag = indexPath.row; 
secondArgument = indexPath.section; 
[button addTarget:self action:@selector(printMethod:) forControlEvents:UIControlEventTouchUpInside]; 

-(IBAction)printMethod:(UIButton*)sender{ 
    NSLog(@"%d%d",sender.tag,//SecondArgument); 
} 
+1

당신은 할 수 없습니다에 패스 두 번째 인수에 대한 accessibilityIdentifier을보십시오. 당신이 할 수있는 일은 UIButton을 서브 클래스 화하고 속성을 제공하는 것입니다 (indexPath, otherDataToShare). – Larme

+0

IBAction 메서드는 단일 인수 만 허용합니다. 보낸 사람 – Paulw11

+0

두 번째 인수는 무엇입니까? –

답변

1

indexPath 버튼 동작을 원할 경우 다음과 같이 시도하십시오.

-(IBAction)printMethod:(UIButton*)sender{ 

    CGPoint point = [sender.superview convertPoint:sender.center toView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; 
    if (indexPath) { 
      NSLog(@"Section - %ld Row - %ld",deleteIndexPath.section, deleteIndexPath.row); 
    } 
} 
+0

하지만 Arpit B Parekh의 의견이 당신의 대답보다 낫습니다. 여기서 당신은 인수로 객체를 전달할 수도 있습니다; –

+1

@AMIamitekh 나는 그 접근법을 한번도 시도한 적이 없으므로 그것에 대해 모른다. –

1

그냥 서브 클래스 UIButton 당신이 매개 변수로 전달하려는 클래스의 속성을 추가 할 수 있습니다.

그러면 해당 버튼의 인스턴스로 평가할 수 있습니다. 예를 들어,

#import <UIKit/UIKit.h> 

@interface CustomFileCapturebutton : UIButton 

@property int maxNumberOfFilesAllow; 
@property NSString *fileType; 

@end 

는, CustomFileCapturebutton의 인스턴스를 생성하고 같은 액션 무언가를 창조

-(void)captureClick : (CustomFileCapturebutton*)sender{ 

    // you can use your properties here like 



    NSLog (@"%@",sender.fileType); 

} 
을 설정할 수 있습니다

그 당시 등록 할 때 addtarget 같은 버튼,

CustomFileCapturebutton *btn = [[CustomFileCapturebutton alloc]init]; 

    btn.frame = yourFrame; 

    [btn addTarget:self action:@selector(captureClick:) forControlEvents:UIControlEventTouchUpInside]; 

    btn.fileType = @"png"; // set properties here 
1

버튼

button.accessibilityIdentifier = [NSString stringWithFormat:@"%d",indexPath.section)];