2013-10-31 4 views
0

Xcode 5 버튼 문제가 있습니다.버튼 클릭 등록 안함

UIViewController에 프로그래밍 방식으로 버튼을 만든 다음이 버튼에 클릭을 등록하려고합니다. NSLog을 사용하여 클릭 수를 추적하지만 클릭 수가 로그에 등록되지 않은 것으로 보입니다.

아무도 도와 줄 수 있습니까?

각 버튼을 클릭 할 때마다 UIViewController에 도달하는 궁극적 인 목표는 버튼 클릭이 먼저 등록되었는지 확인하기위한 테스트였습니다.

여기에 버튼을 만드는 방법은 viewDidLoad (1 개만 표시)입니다. 참고 : if-statement은 이전보기에서 어떤 단추가 눌러 졌는지 확인하는 데에만 사용됩니다. 내가

- (void) buttonClicked 
{ 
    NSLog(@"Button %i clicked", hotelButton.tag); 
} 

참고 버튼 클릭을 확인하는 방법을 여기

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 

    NSString *myString = [NSString stringWithFormat:@"%d", self.tagNumber ]; 

    if (self.tagNumber == 1) 
    { 
     hotelButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     hotelButton.tag = 1; 
     hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f); 

     UIImage *buttonImage1 = [UIImage imageNamed:@"buttonImage1.png"]; 
     [hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal]; 
     [self.view addSubview:hotelButton]; 

     // Add targets and actions 
     [hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside]; 
    } 

}

그리고이 : 나는 또한 다음과 같은 시도 :

- (void) buttonClicked:(UIButton *) button 
{ 
    NSLog(@"Button %ld clicked", (long int)[button tag]); 
} 

을하지만 그 "선언되지 않은 오류를 준 선택자 buttonClicked "에 @viewDidLoad @ action : @ selector로 지정) :

[hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside]; 

답변

3

사용이 애플 문서에서

[hotelButton addTarget:self action:@selector(buttonClicked) 
    forControlEvents:UIControlEventTouchUpInside]; 

:

UIControlEventTouchUpInside 
A touch-up event in the control where the finger is inside the bounds of the control. 

UIControlEventTouchUpOutside 
A touch-up event in the control where the finger is outside the bounds of the control 

그리고이 방법을 계속 :

- (void) buttonClicked 
{ 
    NSLog(@"Button %i clicked", hotelButton.tag); 
} 
+0

잘 보니, 완전히 빠졌습니다. 틀린 것을 입력해야합니다. 감사. – heyred

1

이 시도를,

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

하고 보낸 사람을 통과

,

- (void) buttonClicked:(UIButton*)sender 
{ 
    NSLog(@"Button %i clicked", [sender tag]); 
} 
1

시도하고 내부로 외부의 변경,

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
1

는 경우 조건을 수정했습니다. 따르십시오 : -

if (self.tagNumber == 1) 
    { 
     hotelButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     hotelButton.tag = 1; 
     hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f); 

     UIImage *buttonImage1 = [UIImage imageNamed:@"buttonImage1.png"]; 
     [hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal]; 
hotelButton.showsTouchWhenHighlighted = YES; 
hotelButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10); 
     [hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; 
     [self.view addSubview:hotelButton]; 

    }