Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 视图控制器的委派和解除_Iphone_Objective C_Xcode - Fatal编程技术网

Iphone 视图控制器的委派和解除

Iphone 视图控制器的委派和解除,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我正在开发一个应用程序,它有一个UITableViewController,其中填充了一个产品列表。每一行都连接到一个UIViewController,该UIViewController显示该行中产品的详细信息。现在,由于点击每一行并返回查看下一个产品的详细信息对用户来说可能太乏味了,我们决定添加此功能:当用户在产品的UIViewController上滑动时,将推送包含下一个产品详细信息的UIViewController 但是,到目前为止,我不确定实现这一点的最佳方式。我试图将产品数组传递给UI

我正在开发一个应用程序,它有一个UITableViewController,其中填充了一个产品列表。每一行都连接到一个UIViewController,该UIViewController显示该行中产品的详细信息。现在,由于点击每一行并返回查看下一个产品的详细信息对用户来说可能太乏味了,我们决定添加此功能:当用户在产品的UIViewController上滑动时,将推送包含下一个产品详细信息的UIViewController

但是,到目前为止,我不确定实现这一点的最佳方式。我试图将产品数组传递给UIViewController,以便实现滑动,但这将违反MVC框架,对吗?视图不能拥有它们显示的数据。产品详细信息UIViewController应该只知道传递给它的特定产品,而不是其他产品,对吗

我认为这可以通过授权来实现,但我不确定如何实现。有人能帮我吗?谢谢

编辑: Rob Mayoff的代码非常有用,所以我决定实现它。但同时,我将使用一个简单的round rect按钮来调用函数,而不是实现滑动

- (IBAction)showNextProduct:(id)sender {
    [self.productsTVC goToProductAtIndex:self.productIndex + 1];
}

- (IBAction)showPriorProduct:(id)sender {
    [self.productsTVC goToProductAtIndex:self.productIndex - 1];
}

但每次我点击任何一个按钮,我的应用程序都会崩溃,并显示这样一条消息:
在意外状态下完成导航转换。导航栏子视图树可能已损坏。开始/结束外观转换的调用不平衡。

假设您有一个
CatalogViewController
(它是
UITableViewController
的子类)和一个
ProductViewController
(它是
UIViewController
的子类)

实现“滑动到下一个产品”的最简单方法是为
ProductViewController
提供对
CatalogViewController
的引用。它应该是
(如果使用ARC)或
分配
(如果不使用ARC)。您还需要一个在目录中保存产品索引的属性:

@interface ProductViewController

@property (nonatomic, weak) CatalogViewController *catalogViewController;
@property (nonatomic) NSInteger productIndex;

@end
@implementation ProductViewController

- (IBAction)showNextProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex + 1];
}

- (IBAction)showPriorProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex - 1];
}
然后在刷卡的操作方法中,向
CatalogViewController
发送一条消息,要求它转到目录中的下一个(或上一个)产品:

@interface ProductViewController

@property (nonatomic, weak) CatalogViewController *catalogViewController;
@property (nonatomic) NSInteger productIndex;

@end
@implementation ProductViewController

- (IBAction)showNextProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex + 1];
}

- (IBAction)showPriorProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex - 1];
}
CatalogViewController
中,无论何时创建
ProductViewController
,都需要设置以下属性:

@implementation CatalogViewController

- (ProductViewController *)productViewControllerForProductAtIndex:(NSInteger)index {
    if (index < 0 || index >= self.products.count)
        return nil;
    ProductViewController *vc = [[ProductViewController alloc] initWithProduct:[self.products objectAtIndex:index]];
    vc.catalogViewController = self;
    vc.productIndex = index;
    return vc;
}
可以使用相同的方法处理表格行选择:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self goToProductAtIndex:indexPath.row];
}

如果您想获得更多的软件工程,可以围绕
goToProductAtIndex:
方法创建一个协议,并使用该协议避免
ProductViewController
了解
CatalogViewController
类。

假设您有一个
CatalogViewController
(它是
UITableViewController
的子类)和
ProductViewController
(它是
UIViewController
的子类)

实现“滑动到下一个产品”的最简单方法是为
ProductViewController
提供对
CatalogViewController
的引用。它应该是
weak
(如果使用ARC)或
assign
(如果不使用ARC)。您还需要一个在目录中保存产品索引的属性:

@interface ProductViewController

@property (nonatomic, weak) CatalogViewController *catalogViewController;
@property (nonatomic) NSInteger productIndex;

@end
@implementation ProductViewController

- (IBAction)showNextProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex + 1];
}

- (IBAction)showPriorProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex - 1];
}
然后在刷卡的操作方法中,向
CatalogViewController
发送一条消息,要求它转到目录中的下一个(或上一个)产品:

@interface ProductViewController

@property (nonatomic, weak) CatalogViewController *catalogViewController;
@property (nonatomic) NSInteger productIndex;

@end
@implementation ProductViewController

- (IBAction)showNextProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex + 1];
}

- (IBAction)showPriorProduct:(id)sender {
    [self.catalogViewController goToProductAtIndex:self.productIndex - 1];
}
CatalogViewController
中,无论何时创建
ProductViewController
,都需要设置以下属性:

@implementation CatalogViewController

- (ProductViewController *)productViewControllerForProductAtIndex:(NSInteger)index {
    if (index < 0 || index >= self.products.count)
        return nil;
    ProductViewController *vc = [[ProductViewController alloc] initWithProduct:[self.products objectAtIndex:index]];
    vc.catalogViewController = self;
    vc.productIndex = index;
    return vc;
}
可以使用相同的方法处理表格行选择:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self goToProductAtIndex:indexPath.row];
}

如果你想获得更多的软件工程师,你可以围绕
goToProductAtIndex:
方法创建一个协议,并使用该协议避免
ProductViewController
了解
CatalogViewController
类。

谢谢@rob。我将尝试此解决方案,并让你知道。谢谢!rob,我收到一条错误消息:“正在以意外状态完成导航转换。导航栏子视图树可能已损坏。”和”用于开始/结束外观转换的不平衡调用,以确保所有
视图将出现:
视图显示:
视图将消失:
,和
视图消失:
方法调用它们的
超级
方法。我怀疑这是否更容易。如果您有多个产品,可能需要动态加载产品视图,并将其添加到滚动视图中,以避免使用太多内存。您应该观看WWDC 2010中的“使用滚动视图设计应用程序”视频和“高级滚动视图技术”来自WWDC 2011的视频。如果你想将iOS 5作为目标,你可以使用
UIPageViewController
让它变得非常性感。这在WWDC 2011的“实现UIViewController遏制”视频的最后15分钟中有介绍。谢谢@rob。我将尝试此解决方案,并让你知道。谢谢!rob,我收到一条错误消息:“正在以意外状态完成导航转换。导航栏子视图树可能已损坏。”和”用于开始/结束外观转换的不平衡调用,以确保所有
视图将出现:
视图显示:
视图将消失:
,和
视图消失:
方法调用它们的
超级
方法。我怀疑这是否更容易。如果您有多个产品,可能需要动态加载产品视图,并将其添加到滚动视图中,以避免使用太多内存。您应该观看WWDC 2010中的“使用滚动视图设计应用程序”视频和“高级滚动视图技术”来自WWDC 2011的视频。如果你想瞄准iOS 5,你可以使用
UIPageViewController
让它变得非常性感。WWDC 2011“实现UIViewController包容”视频的最后15分钟将介绍这一点。