2017-02-21 88 views
0

我從今天開始開始使用IOS,並開始在現有項目上做一些小的改動。通過界面生成器,我可以添加一個按鈕。現在,當點擊該按鈕時,我想從不同的故事板打開另一個控制器。IOS - 點擊按鈕時打開另一個控制器

這是我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 

這是我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 

我在谷歌搜索,是無法理解。所以我在這裏尋求你的幫助。我正在使用xcode8。

+0

Th易於使用的解決方案是在UItabelViewCell的「cellForRowAtIndexPath」instread中將按鈕目標添加到ViewController。你可以在這篇文章中找到解決方案http://stackoverflow.com/questions/15652133/how-to-set-action-for-uibutton-in-uitableviewcell –

+0

@Sandeep庫馬爾你已經鏈接到的答案是有一個表格視圖單元格中的按鈕。操作在其單元格中沒有按鈕 – Gruntcakes

+0

「我想從不同的故事板打開另一個控制器」這是正確的嗎?您今天只能盯着iOS,但您的項目中已經有不止一個故事板。似乎有點過於進步第一天 – Gruntcakes

回答

0

其實你正在使用的tableview在project.so你必須設置按鈕輕擊動作是這樣的:

- (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

如果您需要檢查哪裏按鈕按下電池和一些數據與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 *)cell;對於這一行,我收到錯誤「預期類型」! –

+0

爲您添加簡單的項目:https://github.com/OMGHaveFun/test4 – OMGHaveFun

相關問題