简体   繁体   English

一个viewController的多个实例以在另一个viewController中引用

[英]multiple instances of a viewController to reference in a different viewController

I have a viewController which takes details of a product and calculates the price. 我有一个viewController,它获取产品的详细信息并计算价格。 The User can calculate the price and add that product to cart. 用户可以计算价格并将该产品添加到购物车。 Here I was simply adding self to the array defined in cart.h and user can continue to be on the same screen, calculate price for another product and add to cart. 在这里,我只是将self添加到cart.h中定义的数组中,并且用户可以继续在同一屏幕上,计算另一种产品的价格并添加到购物车中。 If I have added 3 different products to the cart, in the cartViewController I see 3 products added, but the details of all the 3 products is the same(ie details of the 3rd products are shown) 如果我在购物车中添加了3种不同的产品,则在cartViewController中我看到添加了3种产品,但是所有3种产品的详细信息都相同(即,显示了第3种产品的详细信息)

Is there any way by which I can keep track of what product I am adding? 有什么方法可以跟踪要添加的产品?

Thanks in advance for your help. 在此先感谢您的帮助。

Details : I have a product screen. 详细信息:我有一个产品屏幕。 There are 5 textfields which take in values and calculate final price. 有5个文本框,用于输入值并计算最终价格。 User can tap "Add to Cart" to add the details of this product to the cart and continue on the same screen to find the price of next product by changing the textfield values. 用户可以点击“添加到购物车”以将该产品的详细信息添加到购物车,并在同一屏幕上继续以通过更改文本字段值查找下一个产品的价格。 Once again the user taps on "Add to Cart" to add details of the new product and so on. 用户再次点击“添加到购物车”以添加新产品的详细信息,依此类推。 When user taps of "View Cart" he should see all the products he had added, but I see only the last product added multiple times, ie every time I add a new product, it overwrites the previous products as well. 当用户点击“查看购物车”时,他应该看到自己添加的所有产品,但是我只能看到多次添加的最后一个产品,即每次添加新产品时,它也会覆盖以前的产品。 So, as a result I am only able to see the last product the no.of times I tapped "Add to Cart" 因此,结果是我只能点击“添加到购物车”的次数才能看到最后一个产品

Please help. 请帮忙。

You're probably using an instance variable to build the cart item and adding it to the array. 您可能正在使用实例变量来构建购物车商品并将其添加到数组中。 Then you're editing it and adding it again. 然后,您正在对其进行编辑并再次添加。 And again. 然后再次。

You should be creating a new cart item each time and adding the new item. 您应该每次都创建一个新的购物车商品并添加新商品。 Then you won't edit it the next time the button is tapped. 这样,下次单击该按钮时就不会对其进行编辑。

Never mind, I found a workaround for it. 没关系,我找到了解决方法。 I am creating a model class and creating a new instance of that class every time I need to add an instance of the viewController to the array. 每当我需要将viewController的实例添加到数组时,我都会创建一个模型类并创建该类的新实例。

May be it is not the optimum way but it is working for me. 可能这不是最佳方法,但对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM