0

I have a list of deals in an NSMutableArray. I want to remove one of these deals in my redeem class. How do I do so? I use tableviewcells in a UITableViewController to display my deals using the count method.

How can I use the IBAction of the redeem button to remove a deal from the deal array?

Much help is needed this is very important, thanks.

//  AppDealsTVC.m
#import "AppDealsTVC.h"

- (void)viewDidLoad {
[super viewDidLoad];


    NSDictionary *dealOne = @{kDeal: @"25% off Club Sandwich",
                          kDiscount: @"now $5.25",
                          kDImage: @"Club_San_RA.jpg",
                          kDDescription: @"Get 25% off our flavorful Club Sandwich!",
                          };

    NSDictionary *dealTwo = @{kDeal: @"Grilled Cheese",
                          kDiscount: @"FREE (read more)",
                          kDImage: @"grilled_cheese_RA.jpg",
                          kDDescription: @"Our flavorful Club Sandwich offers a variety of tastes combined into one gorgeous looking and great tasting sandwich!",
                          };

    NSDictionary *dealThree = @{kDeal: @"Club Sandwich w fries/chips",
                          kDiscount: @"FREE (read more)",
                          kDImage: @"Club_San_RA.jpg",
                          kDDescription: @"Our flavorful Club Sandwich offers a variety of tastes combined into one gorgeous looking and great tasting sandwich!",
                          };


self.dealListArray = [NSMutableArray arrayWithObjects:

                       dealOne,
                       dealTwo,
                       dealThree,

                       nil];
}



//  RedeemVC.m

#import "RedeemVC.h"
#import "AppDealsTVC.h"

- (IBAction)redeemButton:(id)sender {

// Remove deal from the self.dealListArray

}
srinivas n
  • 640
  • 4
  • 20
Peter Schultz
  • 219
  • 1
  • 11

2 Answers2

0
  1. You dont need arrayWithObjects method. Just: self.dealListArray = @[dealOne, dealTwo, dealThree];

  2. This button is inside of your cell? Most elagant approach is use Delegation Pattern: Adding a delegate to a custom UITableViewCell (bad access error)

Community
  • 1
  • 1
Klevison
  • 3,342
  • 2
  • 19
  • 32
0

To communicate between different ViewControllers, you will want to use NSNotificationCenter.

In your AppDealsTVC you would call [NSNotificationCenter postNotificationName:@"RedeemDeal" object:self userInfo:deal] when you want to remove a deal, and in your viewDidLoad method of RedeemVC you should subscribe to this notification with [NSNotificationCenter addObserver:self selector:@selector(methodNameToRemoveDeal:) notificationName:@"RedeemDeal" notificationSender:nil]

This will send the deal as a parameter to the method methodNameToRemoveDeal:, where you can remove it. You will need to implement a different remove method, rather than calling the redeemButton:sender method directly, but you can reuse the same redeeming code by calling methodNameToRemoveDeal: from redeemButton:sender in your RedeemDealVC

You will also need to be sure to call [NSNotificationCenter removeObserver:self name:@"RedeemDeal" object:nil] in the [dealloc] method of RedeemVC in order to unsubscribe from the event when RedeemVC is deallocated from the stack in order to prevent a memory leak.

I would also suggest defining the @"RedeemDeal" notification name as a constant somewhere in your application to avoid hardcoding the name many different places in the application. Makes it easier to change later in a single location if need be.

And while I'm making design suggestions.... it may be a good idea to define a Deal class in your model rather than relying on dictionary representations of your deals. This way you can more clearly define the different parts of the deal as properties and can include methods such as - (void) redeem to handle the redeeming of your deals.

rfj001
  • 7,948
  • 8
  • 30
  • 48
  • 1
    No no no... He will want deal with "Delegates". Notification just to update and array? Look at this: http://stackoverflow.com/questions/17921155/nsnotificationcenter-vs-delegation-which-is-faster and this http://stackoverflow.com/questions/1927965/nsnotificationcenter-vs-delegation-using-protocols – Klevison Jul 29 '15 at 23:41
  • Delegates can work if he has a reference to the `RedeemVC` inside the `AppDealsTVC`. Judging from the name of the class I assume it is a separate view controller, not a `UITableViewCell` subclass found inside the `AppDealsTVC`. Under this assumption the two ViewControllers will be far less coupled and dependent on each other by using the notification pattern. Also, as a one-off event the performance differences between delegation and notifications would be negligible. – rfj001 Jul 29 '15 at 23:49