2017-02-21 81 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

相关问题