gpt4 book ai didi

ios - 如何在 Objective-C 中从原型(prototype)单元引用父 UIViewController?

转载 作者:行者123 更新时间:2023-11-28 19:49:24 25 4
gpt4 key购买 nike

我有一个幻灯片菜单。它是使用组件 SWRevealViewController 实现的。为了实现它,我有 1 个主要的 UIViewController (VC) - SWRevealViewController。我有 menu VC 并且我有 push segues 导航到不同的 menu VC .

对于菜单,我为每个菜单使用带有自定义类的原型(prototype)单元格。

我的问题是我需要调用 unwind seguelogin VC,使用 alert view .为此,我尝试使用常用方法 [self performSegueWithIdentifier:@"unwSegReturnToLogin"sender:self];alert view 得到肯定的回答(在 exit 的自定义类中 单元格)。我在我的 login VC 中声明了这样的方法。我在编译过程中收到错误:

No visible @interface for 'tvcellExitMenuItem' declares 
the selector 'performSegueWithIdentifier:sender:'

我怀疑问题是 self 在我的例子中是 table cell 而不是 UIViewController

如果是这种情况,如何引用父 VC

如果不对,请告诉我逻辑哪里错了。

最佳答案

subview 不应该知道它的父 View Controller 。相反,适合您需要的常见模式是 delegate 模式:为您的单元类定义一个委托(delegate)属性和协议(protocol)。

// your cell class header might look like this

@class MyCellClass;
@protocol MyCellDelegate

- (void)onCellSelected:(MyCellClass *)cell;

@end

@interface MyCellClass

@property (nonatomic, weak) id<MyCellDelegate> delegate;

@end

例如,如果您的 viewController 也是您的 UITableViewDatasource ,那么在 tableView:cellForRowAtIndexPath: 中,您可以将单元格的委托(delegate)设置为 self,并在委托(delegate)方法中调用 segue 方法。

- (void)onCellSelected:(MyCellClass *)cell
{
// retrieve cell indexPath
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];

// using indexPth, retrieve cell's data

// push segue with data selected
}

当然,这只是一个例子,还有其他正确的做法。

关于ios - 如何在 Objective-C 中从原型(prototype)单元引用父 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com