xcode에서 프로그래밍 방식으로 버튼에 액션을 추가하는 방법
인터페이스 빌더에서 드래그하여 버튼에 IB 액션을 추가하는 방법으로 앞뒤로 전환하는 것을 추가하기위한 방법으로 작업을 계획하는 방식으로 전환하는 것을 추가하고 싶습니다. 해결은 아마도 정말 간단 할 것입니다.하지만 검색 할 때 답을 수없는 것입니다. 감사합니다!
이 시도 :
스위프트 4
myButton.addTarget(self,
action: #selector(myAction),
for: .touchUpInside)
목표 -C
[myButton addTarget:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
Apple의 문서에서 풍부한 정보 소스를 사용할 수 있습니다. 상기 봐 가지고 있는 UIButton의 설명서를 , 그것은 존재하는 UIButton 가의 후손 발표 할 예정이다 UIControl, 할 수있는 방법을 구현하는 목표를 추가하십시오 .
-
콜론 추가 여부에주의를 기울 myAction
입니다.action:@selector(myAction)
여기에 참조가 있습니다.
신속한 답변 :
myButton.addTarget(self, action: "click:", for: .touchUpInside)
func click(sender: UIButton) {
print("click")
}
문서 : https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget
CGRect buttonFrame = CGRectMake( 10, 80, 100, 30 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"My Button" forState: UIControlStateNormal];
[button addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[view addSubview:button];
CGRect buttonFrame = CGRectMake( x-pos, y-pos, width, height ); //
CGRectMake(10,5,10,10)
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button setTitle: @"My Button" forState: UIControlStateNormal];
[button addTarget:self action:@selector(btnClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor: [UIColor BlueVolor] forState:
UIControlStateNormal];
[view addSubview:button];
-(void)btnClicked {
// your code }
이 시도 :
먼저 viewcontroller의 .h 파일에 작성하십시오.
UIButton *btn;
이제 viewcontrollers viewDidLoad의 .m 파일에 작성하십시오.
btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[btn setBackgroundColor:[UIColor orangeColor]];
//adding action programatically
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
이 외부 viewDidLoad 메소드를 뷰 컨트롤러의 .m 파일에 작성하십시오.
- (IBAction)btnClicked:(id)sender
{
//Write a code you want to execute on buttons click event
}
Swift 3의 경우
먼저 버튼 동작에 대한 함수를 만든 다음 버튼 대상에 함수를 추가하십시오.
func buttonAction(sender: UIButton!) {
print("Button tapped")
}
button.addTarget(self, action: #selector(buttonAction),for: .touchUpInside)
UIButton *btnNotification=[UIButton buttonWithType:UIButtonTypeCustom];
btnNotification.frame=CGRectMake(130,80,120,40);
btnNotification.backgroundColor=[UIColor greenColor];
[btnNotification setTitle:@"create Notification" forState:UIControlStateNormal];
[btnNotification addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnNotification];
}
-(void)btnClicked
{
// Here You can write functionality
}
UIButton
에서 상속 UIControl
합니다. 여기에는 작업을 추가 / 제거하는 모든 방법이 있습니다. UIControl 클래스 참조 . , 커버 "액션 메시지 준비 및 전송"섹션 봐 – sendAction:to:forEvent:
와 – addTarget:action:forControlEvents:
.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
'IT' 카테고리의 다른 글
Rails 형식화 날짜 (0) | 2020.08.13 |
---|---|
Git의 유명한 "오류 : .git에 대한 권한이 사용자에게 거부되었습니다." (0) | 2020.08.13 |
Android Studio- 디버그 키 저장소 (0) | 2020.08.13 |
CSS 배경 이미지를 어둡게 하시겠습니까? (0) | 2020.08.13 |
Javascript를 통해 이미지의 평균 색상 (0) | 2020.08.13 |