【问题标题】:Go to another view by clicking a button in Custom Cell通过单击自定义单元格中的按钮转到另一个视图
【发布时间】:2015-10-06 03:05:18
【问题描述】:

我将自定义单元格 (XIB) 创建为 UICollectionViewCell 的子类,并且该单元格中有一个按钮。当我单击一个按钮时,我想转到另一个带有一些数据的视图,并且也可以通过单击一个按钮返回到原始视图。我已经搜索并找到了类似“segue”或“modal”的内容,但我最初无法从我的自定义单元格中执行此操作。

有没有办法做到这一点?任何帮助将不胜感激。

【问题讨论】:

  • 你的问题是什么?向您的单元格添加一些属性,为您提供启动和启动新视图所需的所有信息。是的,您可以以模态方式呈现它,或者您可以将视图控制器推送到导航控制器,或者您可以使用情节提要中的转场。
  • @HermannKlecker 我没有使用故事板,只有 XIB。没有“触发转场”作为情节提要。
  • 好吧,那你不能使用segue。还有两个选项。
  • @user1967709 是的,就这样

标签: ios objective-c xib uicollectionviewcell


【解决方案1】:

所以你想要做的是,因为 UICollectionView 的工作方式与 UITableView 相同,所以你要做的是创建一个 UICollectionViewCell 的子类,其中包含一个 protocol 以将操作(如按下按钮)发送到视图控制器从不同的角度。在这种情况下,另一个视图是 UICollectionViewCell。

向 UICollectionViewCell 添加协议

添加一个名为 UICustomCollectionViewCell 的新 Cocoa Touch 类,其子类为 UICollectionViewCell。并包含界面构建器文件

头文件 UICustomCollectionViewCell.h

@protocol UICustomCollectionViewCellDelegate;

@interface UICustomCollectionViewCell : UICollectionViewCell

@property ( nonatomic, retain) IBOutlet UIButton *button;
- (IBAction)pressButton:(id)sender;

@property ( assign) id< UICustomCollectionViewCellDelegate> delegate;

@end

@protocol UICustomCollectionViewCellDelegate <NSObject>

@optional
- (void)customCollectionViewCell:(UICustomCollectionViewCell *)cell pressedButton:(UIButton *)button;

@end

实现文件 UICustomCollectionViewCell.m

@implementation UICustomCollectionViewCell
@synthesize delegate;

- (IBAction)pressButton:(id)sender {
    if ([delegate respondsToSelector: @selector( customCollectionViewCell:pressedButton:)])
        [delegate customCollectionViewCell: self pressedButton: sender];

}

@end

xib 文件 UICustomCollectionViewCell.xib

确保来自 UICustomCollectionViewCell 的连接已连接到来自 Connections Inspector 的按钮:

  1. 按钮
  2. -pressButton:

最后,在你的项目中使用这个类

导入类和委托:

#import "UICustomCollectionViewCell.h"

@interface ViewController () < UICustomCollectionViewCellDelegate>

@end

在以下代码中,您将使用 UICustomCollectionViewCell 类而不是 UICollectionViewCell

UICustomCollectionViewCell *cell;

...
[cell setDelegate: self];
...

return cell;

现在是按下按钮时调用的动作或方法:

- (void)customCollectionViewCell:(UICustomCollectionViewCell *)cell pressedButton:(UIButton *)button {
    //action will be here when the button is pressed

}

如果你想知道这个单元格来自哪个 indexPath:

[collectionView indexPathForCell: cell];

【讨论】:

  • 请务必要求对此进行任何解释。我很高兴马上回复:)
  • 我已经看到你的答案,现在正在尝试,如果有效会回复。 :)
  • 我想知道新视图是否需要成为UICollectionViewCell 的子类以及如何使用presentViewController 转到该视图不起作用。
  • 当您的意思是“新视图”时,您是否试图将这个“新视图”呈现为视图控制器?不确定您要展示什么
  • 你会想要创建一个名为 SettingsViewController 的新类,并且还需要 包含 xib 文件。然后将这个新类导入主视图控制器的实现文件,然后将此代码粘贴到按下按钮时的操作位置:SettingsViewController *viewSettings = [[SettingsViewController alloc] initWithNibName: @"SettingsViewController" bundle: [NSBundle mainBundle]]; 这将分配您的新视图控制器。然后将其粘贴到下一行:[self presentViewController: viewSettings animated: YES completion: ^{ }]; 然后这将显示设置
【解决方案2】:

您不能/不应该在单元格中执行导航作业,导航不在单元格域中。

你可以尝试的是

1) 使用委托,设置委托并将其连接到按钮操作,托管 tableview/collection 视图的控制器可以将自己设置为委托并监听任何事件。该控制器应该负责使用您想要的任何方法将新视图推送到堆栈。

2) 如果你讨厌代理但喜欢块,你可以在单元格上设置一个回调块,它的动作可以在控制器的 cellForRowAtIndex: 方法中设置。

注意到这里的模式了吗?上述两种方法都将任务从单元委托给控制器。

如果一切都失败了,只需实现didSelectItemAtIndexPath: 并坚持下去。

【讨论】:

    【解决方案3】:

    你尝试过 didSelect 方法吗?

    - (void)collectionView:(UICollectionView *)collectionView
    didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    
    YourNewViewControllerClass *someViewController = [storyboard instantiateViewControllerWithIdentifier:@"YourNewVCID"];
    
    [self presentViewController:someViewController
                         animated:YES
                       completion:nil];
    }
    

    【讨论】:

      【解决方案4】:

      最简单的方法是实现cellForRow.. 方法,为您的单元格/按钮设置标签并根据该标签做出反应(例如indexPath.row)。

      【讨论】:

        【解决方案5】:

        1.自定义您的按钮

        NouMapButton.h

        #import <Foundation/Foundation.h>
        
        @interface NouMapButton : UIButton 
        @property (nonatomic, readwrite, retain) NSObject *dataObj;
        @end
        

        NouMapButton.m

        #import "NouMapButton.h"
        @implementation NouMapButton
        @end
        
        1. 中设置你的按钮数据和目标
          -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
          
          btn01.dataObj = YOUR_DATA;
          [btn01 addTarget:self action:@selector(map:) forControlEvents:UIControlEventTouchUpInside];
          
        2. 那么就可以在sender.dataObj中获取按钮自定义dataObj

          -(void)map:(NouMapButton *)sender{
              MapViewController *nextView = [[MapViewController alloc] init];
              nextView.dataObj = sender.dataObj;
              //TODO....
          
          
          }
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-03
          相关资源
          最近更新 更多