【问题标题】:IOS - Open another controller when button is tappedIOS - 点击按钮时打开另一个控制器
【发布时间】:2017-07-11 15:05:01
【问题描述】:

我从今天开始自己在 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。

【问题讨论】:

  • 简单的解决方案是在 UItabelViewCell 的 "cellForRowAtIndexPath" instread 中将按钮目标添加到 ViewController。您可以在这篇文章中找到解决方案stackoverflow.com/questions/15652133/…
  • @Sandeep Kumar 您链接到的答案是表格视图单元格中有一个按钮。操作员的单元格中没有按钮
  • " 我想从不同的故事板打开另一个控制器" 这是正确的吗?您今天只关注 iOS,但您的项目中已经有多个故事板。第一天似乎有点太早了
  • IBAction 附加到哪个 UIControll 元素?
  • 过去有数百个关于如何做到这一点的问题,例如这里有几个只需 5 秒的努力搜索即可找到stackoverflow.com/questions/8924052/…brainwashinc.com/2014/01/08/…

标签: ios objective-c


【解决方案1】:

实际上,您在项目中使用的是 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];

}

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • - (void)showDataFromCell:(HomeTableViewCell *)cell;对于这一行,我收到错误“预期类型”!!!
    猜你喜欢
    • 2013-11-03
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    相关资源
    最近更新 更多