IOS - 点击按钮时打开另一个控制器

时间:2017-02-21 19:53:13

标签: ios objective-c

我从今天开始从事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。

2 个答案:

答案 0 :(得分:0)

实际上你正在项目中使用tableview。所以你必须设置按钮点击动作,如下所示:

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

}

此代码将在其他屏幕上导航您。

答案 1 :(得分:0)

如果您需要检查按下按钮的单元格并将一些数据发送到另一个控制器

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

带有TableView的控制器

@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];
}
相关问题