IOS:在自定义 View Controller 和 View Controller 之间传回数据

标签 ios iphone

<分区>

我有两个 View Controller 。 第一个是自定义 View Controller ,它从 Assets 库加载图像。 第二个 View Controller 显示带有取消和删除按钮的所选图像的完整尺寸

我使用下面的代码从自定义 View Controller 中删除选定的图像。 自定义 View Controller .m

-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths
{
//here i want to control the delete option when cancel pressed
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSIndexPath *itemPath in itemPaths) {
[indexSet addIndex:itemPath.row];
}
[self.selectedAssets removeObjectsAtIndexes:indexSet];
}
/* call the delete function*/
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
    NSArray *selectedItemsIndexPaths = [self.collectionview1 indexPathsForSelectedItems];
    [self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths];
    [self.collectionview1 deleteItemsAtIndexPaths:selectedItemsIndexPaths];
    }

这适用于从自定义 View Controller 中删除所选图像。 但它也可以取消。现在,我想在取消时控制删除。 请帮我解决这个问题。

我已经尝试使用按钮标签来识别按下了哪个按钮。但无法在自定义 vc 中控制 secondview.m

- (IBAction)CancelPhoto:(id)sender{
 [Cancel setTag:1]; //set tag value at cancel
}

最佳答案

有几种方法可以在 View Controller 之间传递日期。我根据您的要求为您提供了一个非常简单的解决方案:

假设您的所有图像都在第一个 View Controller 中的可变数组中。

NSMutableAray *imagesArray;

在第二个 View Controller 中创建一个 NSMutableArray 类型的属性图像数组。

@property(nonatomic, strong) NSMutableAray *imagesArray;

在推送/呈现 View 时将数组分配给属性

secondController.imagesArray = imagesArray;

在删除事件中从数组中删除该图像。

[self.imagesArray removeObjectAtIndex:selectedIndex];

viewDidAppear 方法的viewWillAppear 中关闭/弹出您的第二个 View 并刷新您的第一个 View Controller 。

当您传递了主图像数组的引用时,两个类( View Controller )通过指针共享相同的数组,任何一侧的数组更改都将反射(reflect)在两个屏幕上

关于IOS:在自定义 View Controller 和 View Controller 之间传回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949122/

相关文章:

ios - inAppPurchase 后停止 Flurry Ads,不退出应用程序

ios - 为什么我的 UILabel 文本在 Xcode 8.1 的 Swift3 中没有改变

ios - 保护 iPhone 应用程序的数据馈送

iphone - UITabBar 中的自定义颜色

iphone - Box2D 中抵抗重力但与其他物体发生碰撞的物体。如何?

iphone - iOS 7 Beta - 应用内测试不起作用

ios - 全屏查看图片时解锁方向

ios - GridView 单元格向上滚动到 iOS 7 中的状态栏

iphone - 不通过应用商店发布我的应用?

ios - Facebook SDK for IOS、Swift - 使用多个目标、不同的包标识符等