我担心我的4个视图控制器。第一个是CardWalletViewController,它是一个由用户创建的Card Object填充的表格视图(创建卡片是可以的,因此我不会在此发布代码)。现在,假设此表视图已经填充了Card对象,则一旦用户单击单元格,就会显示另一个名为CardDetailsViewController的视图控制器。这将显示卡具有的当前点以及特权。请注意,当前点是通过UILabel显示的,特权是表视图的单元格。该表视图的单元格值(特权)是静态类型的,来自NSDictionary,然后存储在NSArray中(用于单元格的文本显示)。现在,当我单击一个特权时,将显示PerksDetailsViewController。它包含UILabels,其中显示了特权的名称和所需的要点。这些UILabels值来自以前的ViewController。另外,PerksDetailsVC有一个名为redeem的按钮。单击后,我将转到另一个视图控制器BarCodeViewController,它负责计算。

现在,我关心的是如何通过选定的卡,以及与选定的特权相对应的点。我已经使用委派来执行此操作,但是似乎未设置我的委派对象。

下面是我当前的代码。

CardWalletViewController.h

#import <UIKit/UIKit.h>
#import "Card.h"

@class CardWalletViewController;

@protocol CardWalletDelegate <NSObject>

-(void)cardWalletViewController: (CardWalletViewController *)sender
                withCurrentCard: (Card *) currentCard;

@end

@interface CardWalletViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *myWallet;

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

@end


CardWalletViewController.m

#import "CardWalletViewController.h"
#import "AddCardViewController.h"
#import "Card.h"
#import "CardDetailsViewController.h"

@interface CardWalletViewController ()

@end

@implementation CardWalletViewController

@synthesize delegate = _delegate;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }
    Card *cardDummy = [self.myWallet objectAtIndex:indexPath.row]; //myWallet is an Array where the table cell's values come from
    cell.textLabel.text = cardDummy.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", cardDummy.points];

    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //this method is responsible for showing the details of a selected card

    CardDetailsViewController *details = [self.storyboard instantiateViewControllerWithIdentifier:@"cardDetails"];


    Card *selectedCard = [self.myWallet objectAtIndex:indexPath.row]; // I want this selected card to be accessible until the user clicks another card or during end of program.

    details.myPoints = [NSString stringWithFormat:@"%@", selectedCard.points];

    [self.navigationController pushViewController:details animated:YES];


    [self.delegate cardWalletViewController:self withCurrentCard:selectedCard];

    //checking purposes
    if (!self.delegate) {
        NSLog(@"delegate is nil");
    }
}


CardDetailsViewController.m

#import "CardDetailsViewController.h"
#import "PerksDetailsViewController.h"
#import "Card.h"

@interface CardDetailsViewController ()

@end

@implementation CardDetailsViewController

@synthesize pointsLabel = _pointsLabel;
@synthesize myPoints  = _myPoints;

@synthesize perks = _perks;
@synthesize datasource = _datasource;
@synthesize datasourcePoints = _datasourcePoints;

-(void)setupArray
{
    self.perks = [[NSMutableDictionary alloc] init];
    [self.perks setObject:@"200" forKey:@"10% Discount"];
    [self.perks setObject:@"100" forKey:@"250Php Off"];

    self.datasource = [self.perks allKeys]; //contains perk's description
    self.datasourcePoints = [self.perks allValues]; //contains perk's required points
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

 cell.textLabel.text = [self.datasource objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [self.datasourcePoints objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PerksDetailsViewController *perksDetails = [self.storyboard instantiateViewControllerWithIdentifier:@"detailsOfMyPerks"];

    [self.navigationController pushViewController:perksDetails animated:YES];

    perksDetails.perkDetailsLabel.text = [self.datasource objectAtIndex:indexPath.row];
    perksDetails.pointsLabel.text = [self.perks objectForKey:perksDetails.perkDetailsLabel.text];
}

- (void)viewDidLoad
{

    //show the number of points of the selected Card
    self.pointsLabel.text = self.myPoints;

    [self setupArray];

    [super viewDidLoad];
}


PerksDetailsViewController.m

#import "PerksDetailsViewController.h"
#import "Card.h"
#import "CardWalletViewController.h"
#import "BarcodeViewController.h"

@interface PerksDetailsViewController ()

@end

@implementation PerksDetailsViewController

@synthesize pointsLabel = _pointsLabel;
@synthesize perkDetailsLabel = _perkDetailsLabel;
@synthesize perkDetailText = _perkDetailText;
@synthesize pointsText = _pointsText;
@synthesize delegate = _delegate;
@synthesize pointsRequired = _pointsRequired;


- (IBAction)redeemPressed:(id)sender {
    // get required points of a perk selected
    // cast the NSString value to an NSInteger

     NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
     [f setNumberStyle:NSNumberFormatterDecimalStyle];

     self.pointsRequired = [f numberFromString: (self.pointsLabel.text)];

    NSLog(@"points required by the perk %@", self.pointsRequired);

    [self.delegate perksDetailsViewController:self didPassRequiredPoints:self.pointsRequired];

    if (!self.delegate){
        NSLog(@"delegate is nil");
    }
}


PerksDetailsViewController.h

#import <UIKit/UIKit.h>
#import "BarcodeViewController.h"

@class PerksDetailsViewController;

@protocol PerksDetailsDelegate <NSObject>

- (void)perksDetailsViewController:(PerksDetailsViewController *)sender
             didPassRequiredPoints: (NSNumber *) requiredPoints;

@end

@interface PerksDetailsViewController : UIViewController
{
    NSString *perkDetailText;
    NSString *pointsText;
    IBOutlet UILabel *perkDetailsLabel;
    IBOutlet UILabel *pointsLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *perkDetailsLabel, *pointsLabel;
@property (nonatomic, retain) NSString *perkDetailText, *pointsText;
@property (nonatomic, weak) NSNumber *pointsRequired;

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

@end


现在,这个ViewController是实现两个委托方法的那个

BarCodeViewController.h

#import <UIKit/UIKit.h>

@interface BarcodeViewController : UIViewController

@property (nonatomic, strong) NSNumber *myPoints;

@end


BarCodeViewController.m

  #import "BarcodeViewController.h"
    #import "CardWalletViewController.h"
    #import "Card.h"
    #import "PerksDetailsViewController.h"

    @interface BarcodeViewController () <CardWalletDelegate, PerksDetailsDelegate>

    @end

    @implementation BarcodeViewController

    @synthesize myPoints = _myPoints;
    - (void)perksDetailsViewController:(PerksDetailsViewController *)sender didPassRequiredPoints:(NSNumber *)requiredPoints
    {
        self.myPoints = requiredPoints;
    }

    - (void)cardWalletViewController:(CardWalletViewController *)sender withCurrentCard:(Card *)currentCard
    {
        Card *myCurrentCard = currentCard;
        [myCurrentCard.pointsToDeduct addObject:self.myPoints];

        NSLog(@"contents :%@", [myCurrentCard.pointsToDeduct description]);
    }

@end

最佳答案

首先,您的委托对象属性声明

@property (nonatomic, strong) id <PerksDetailsDelegate> delegate;


应该将其从strong更改为weak,因为您的班级不拥有它。

其次,您永远不要为您的类设置委托对象。这就是为什么它是nil的原因。我的猜测是BarCodeViewController(您的PerksDetailsDelegate协议实现类)应该是CardDetailsViewController的实例变量,您错过了将其设置为perksDetailsdidSelectRowAtIndexPath对象的委托的情况。

编辑。

您应该在BarCodeViewController类中有一个CardDetailsViewController实例(对于下面的示例代码,命名为bcvController):

CardDetailsViewController.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PerksDetailsViewController *perksDetails = [self.storyboard instantiateViewControllerWithIdentifier:@"detailsOfMyPerks"];
    [perksDetails setDelegate:bcvController]; // bcvController should be created before this line
    [self.navigationController pushViewController:perksDetails animated:YES];

    perksDetails.perkDetailsLabel.text = [self.datasource objectAtIndex:indexPath.row];
    perksDetails.pointsLabel.text = [self.perks objectForKey:perksDetails.perkDetailsLabel.text];
}


编辑2

BarCodeViewController.h

#import <UIKit/UIKit.h>
#import "CardWalletViewController.h"
#import "Card.h"
#import "PerksDetailsViewController.h"

@interface BarcodeViewController : UIViewController <CardWalletDelegate, PerksDetailsDelegate>

@property (nonatomic, strong) NSNumber *myPoints;

@end


BarCodeViewController.m

#import "BarcodeViewController.h"

//    @interface BarcodeViewController ()
//
//    @end

@implementation BarcodeViewController

@synthesize myPoints = _myPoints;
- (void)perksDetailsViewController:(PerksDetailsViewController *)sender didPassRequiredPoints:(NSNumber *)requiredPoints
{
    self.myPoints = requiredPoints;
}

- (void)cardWalletViewController:(CardWalletViewController *)sender withCurrentCard:(Card *)currentCard
{
    Card *myCurrentCard = currentCard;
    [myCurrentCard.pointsToDeduct addObject:self.myPoints];

    NSLog(@"contents :%@", [myCurrentCard.pointsToDeduct description]);
}

@end


编辑3

如果要显示BarcodeViewController,则应使用该对象的bcvController。

CardDetailsViewController.m

#import "CardDetailsViewController.h"
#import "PerksDetailsViewController.h"
#import "Card.h"

@implementation CardDetailsViewController

@synthesize pointsLabel = _pointsLabel;
@synthesize myPoints  = _myPoints;

@synthesize perks = _perks;
@synthesize datasource = _datasource;
@synthesize datasourcePoints = _datasourcePoints;
@synthesize bcvController = _bcvController; // new instance variable

// other functions removed for clarity

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PerksDetailsViewController *perksDetails = [self.storyboard instantiateViewControllerWithIdentifier:@"detailsOfMyPerks"];
    [perksDetails setDelegate:bcvController];
    [self.navigationController pushViewController:perksDetails animated:YES];

    perksDetails.perkDetailsLabel.text = [self.datasource objectAtIndex:indexPath.row];
    perksDetails.pointsLabel.text = [self.perks objectForKey:perksDetails.perkDetailsLabel.text];
}

- (void)viewDidLoad
{

    //show the number of points of the selected Card
    self.pointsLabel.text = self.myPoints;

    [self setupArray];

    [self setBcvController:[self.storyboard instantiateViewControllerWithIdentifier:@"myBarcodeVC"]]; // use setter to retain object properly

    [super viewDidLoad];
}

关于objective-c - 如何解决仍然为零的代表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10275562/

10-08 20:40