2014-10-20 5 views
0

을 눌렀을 때 설정 특성 및 사용 나는 다음 2 개 버튼 같은 클래스의 모두가하고있는 UIButton는이 세포에서 내 .H 파일있는 UIButton - 버튼을

@interface CellButton : UIButton 

@property (nonatomic,retain) CellButton *property; 

에 셀 버튼을 불렀다 btn1.property = 0;btn2.property = 1;이 셀을 indexPath의 행으로 설정합니다.

-(void)BtnClicked:(UIButton*)sender{ 
    DiningHallTableViewController *dtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DiningHallTableViewController"]; 
[self.navigationController pushViewController:dtvc animated:YES]; 

    //depending on what button is tapped I want to send the number of the button pressed 
} 

가 어떻게이 수를 전송 할 수 있습니다 :

다음
[cell.Btn1 addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.Btn2 addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside]; 

은 방법 : 버튼을 누를 때

그때 나는이 전화를?

편집

enter image description here

이 내 방법에서

내가

NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 

TwoMealsTableViewCell *cell = (TwoMealsTableViewCell*)[self.tableView cellForRowAtIndexPath:myIP]; 
+1

무엇이 문제입니까? 귀하의 속성은 CellButton이 아닌 int에 입력해야합니다 (@property (비 원자) int 속성). 이 작업을 수행하기 위해 버튼을 서브 클래스 화해야한다는 것은 확실하지 않습니다. 버튼의 태그를 indexPath.row에 설정하고 있습니까? – rdelmar

+0

@rdelmar 예 태그가 이미 사용되고 있습니다. 방금 내 셀의 단추에 속성을 추가하려고합니다. 그래서 내가 처음으로 btn을 눌렀거나 두 번째 버튼을 눌렀는지 알았습니다. 기본적으로 호출 된 메서드에서 어떤 단추를 눌렀는지 알고 싶습니다. –

+0

원하는 경우 버튼의 제목을 확인하여이를 수행 할 수 있습니다. 그런 다음 버튼을 하위 클래스로 만들 필요가 없습니다. – rdelmar

답변

1
이 BtnClicked 내 cellForRowAtIndexPath에
-(void)tableView:(UITableView *)table cellForRowAtIndexPath(IndexPath *):indexPath 
{ 
     // Assuming one section, if not then just take section number into account when getting cell 
     // You should be doing this part already 
     cell = self.contentArray[indexPath.row]; 
     // Set tags on the buttons 
     cell.btn1 = [[CellButton alloc] init]; 
     cell.btn2 = [[CellButton alloc] init]; 
     cell.btn1.property = 0; 
     cell.btn2.property = 1; 

     [cell.btn1 addTarget:self action:@selector(BtnClicked:)forControlEvents:UIControlEventTouchUpInside]; 
     [cell.btn2 addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside]; 

     // do anything else you need to do with the cells 

     return cell 
} 

-(void)BtnClicked:(UIButton *)sender{ 
    DiningHallTableViewController *dtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DiningHallTableViewController"]; 
[self.navigationController pushViewController:dtvc animated:YES]; 

    //depending on what button is tapped I want to send the number of the button pressed 
    if ([sender isKindOfClass[CellButton class]]) { 
     property = ((CellButton *)sender).property; 
    } 

    // Now do whatever you need to do with the number 
} 
나는 또한 당신이 navigat을해야한다고 추가해야

ion via storyboard segues를 사용하여 새로운 VC를 만들고 스택에 밀어 넣는 대신 iOS 5+ 용으로 개발하고 있다고 가정합니다. 스토리 보드 단편을 사용하는 온라인 자습서가 많이 있습니다 (such as this one).

@interface CellButton : UIButton 

@property (nonatomic,retain) CellButton *property; 

당신은 당신이 정말로 그것을 원하는 CellButton, 재산의 종류를 설정하는 :

스크린 샷에서 THIRD 편집

, 당신의 문제는 코드에 있음을 분명 정수 여야합니다. 단지 현재의 모범 사례에 부합하지

@interface CellButton : UIButton 

@property (nonatomic) NSInteger property; 

에 위의 라인을 그리고 당신은 탐색을 수행하는 방법을 변경합니다. 스토리 보드를 사용하지 않는다면 (나는 당신이 왜 그렇게되지 않을지 알 수 없습니다.) 나는 당신의 솔루션이 작동한다고 생각합니다.

@property (nonatomic,retain) CellButton *property;

당신은 자체 참조 &가 나는 것입니다 CellButton 클래스의 내부 유형 CellButton (로 property을 선언하고 :

+0

태그 속성이 이미 사용되고 있으며 뷰 컨트롤러를 내가 사용하는 방식과 어떤 차이가 있습니까? –

+0

질문에 스크린 샷을 추가했습니다. contentArray가 필요하지 않습니다. 'cell = self.contentArray [indexPath.row];' –

+0

'self.contentArray'는 tableView의 컨텐트를 보유하고있는 구조 일뿐입니다 난 그냥 속성을 '보유 (또는 강하게)'속성이 유형의 객체 여야한다는 것을 말하는 오류를 얻으려면 ' – rfj001

0

당신이 특정 오류를 얻고있는 이유는이 라인 놀랐다. 따라서 오류를 -

당신이하지 유형 CellButton의, 원시 형 int의이다 0 또는 1로 설정하는 propertycellForRowAtIndexPath 설정

.

오류를 수정하려면 rfj001에서 설명한대로 & @property 선언을 변경하십시오.

@property (nonatomic, retain) NSInteger property;

또한, ARC를 사용하는 것이 좋습니다. 더 이상 수동 메모리 관리가 필요하지 않습니다.