2017-02-21 1 views
0

저는 오늘부터 IOS에서 작업하기 시작했으며 약간의 변경을하기 위해 기존 프로젝트 작업을 시작했습니다. 인터페이스 빌더를 통해 버튼을 추가 할 수있었습니다. 자, 그 버튼을 두 드리면, 다른 스토리 보드에서 다른 컨트롤러를 열고 싶습니다. IOS - 버튼을 누를 때 다른 컨트롤러 열기

이 내 homecell.h

#import <UIKit/UIKit.h> 

@interface HomeTableViewCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *imageViewHome; 
@property (weak, nonatomic) IBOutlet UILabel *labelHeading; 
@property (weak, nonatomic) IBOutlet UILabel *labelSubHeading; 

- (IBAction)sharetapped:(id)sender; 
@end 

내가 구글에서 검색하고 이해할 수 없었다 내 homecell.m

#import "HomeTableViewCell.h" 
#import "HostListingsViewController.h" 

@implementation HomeTableViewCell 

- (void)awakeFromNib { 
    // Initialization code 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

- (IBAction)sharetapped:(id)sender { 
// here i want to be redirected to another controller 
} 
@end 

입니다. 그래서 여기 당신의 도움을 요청하고 있습니다. 나는 xcode8을 사용하고있다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [cell.buttonname addTarget:self 
         action:@selector(navigate:) forControlEvents:UIControlEventTouchDown]; 
} 

-void(navigate) 
{ 

     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainFlow" bundle: nil]; 
     yourViewController *abcd = [storyboard instantiateViewControllerWithIdentifier:@"yourstoryboard id"]; 

     [self.navigationController pushViewController:outCome animated:YES]; 

} 

이 코드가 다른 화면을 탐색합니다

+0

Th 쉬운 솔루션은 UItabelViewCell의 "cellForRowAtIndexPath"instread의 ViewController에 버튼 대상을 추가하는 것입니다. 이 게시물에서 해결책을 찾으실 수 있습니다. http://stackoverflow.com/questions/15652133/how-to-set-action-for-uibutton-in-uitableviewcell –

+0

@Sandeep Kumar 링크 된 답변은 버튼을 클릭합니다. op에 셀에 단추가 없습니다. – Gruntcakes

+0

"다른 스토리 보드에서 다른 컨트롤러를 열려고합니다."맞습니까? 오늘 iOS를 보았을 때만 프로젝트에 스토리 보드가 두 개 이상 있습니다. 첫날 조금 너무 앞서 보입니다. – Gruntcakes

답변

0

사실 당신은 당신이이 같은 버튼을 탭 동작을 설정해야합니다 귀하의 project.so에있는 tableview를 사용하고 있습니다.

0

당신은 버튼을 누르면 셀을 확인하고있는 TableView

와 다른 컨트롤러에

homecell.h

#import <UIKit/UIKit.h> 

@protocol HomeCellDelegate 

- (void)showDataFromCell:(HomeTableViewCell *)cell; 

@end 

@interface HomeTableViewCell : UITableViewCell 

@property (weak, nonatomic) id <HomeCellDelegate> delegate; 

@property (weak, nonatomic) IBOutlet UIImageView *imageViewHome; 
@property (weak, nonatomic) IBOutlet UILabel *labelHeading; 
@property (weak, nonatomic) IBOutlet UILabel *labelSubHeading; 

- (IBAction)sharetapped:(id)sender; 

@end 

homecell.m

#import "HomeTableViewCell.h" 
#import "HostListingsViewController.h" 

@implementation HomeTableViewCell 

- (void)awakeFromNib { 
    // Initialization code 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

- (IBAction)sharetapped:(id)sender { 
    [self.delegate showDataFromCell:self]; 
} 

@end 

컨트롤러를 일부 데이터를 전송해야하는 경우

@interface YourViewController() <HomeCellDelegate> 

... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
...  
HomeTableViewCell *homeCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell" forIndexPath:indexPath]; 

homeCell.delegate = self; 
... 

- (void)showDataFromCell:(HomeTableViewCell *)cell { 
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
... 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"yourAnotherStoryboard" bundle: nil];   
yourViewController *presentController = [storyboard instantiateViewControllerWithIdentifier:@"yourViewController"]; 
presentController.someDataFromCell = someDataArray[indexPath.row]; 
      [self presentViewController:presentController 
           animated:NO 
          completion:nil]; 
} 
+0

- (void) showDataFromCell : (HomeTableViewCell *) 셀; 이 줄에서는 "예상 된 유형"오류가 발생합니다 !!! –

+0

간단한 프로젝트 추가 : https://github.com/OMGHaveFun/test4 – OMGHaveFun

관련 문제