0

私のプログラムは、タイマー付きの画像を表示します。ユーザーが UISlider を使用してタイマーを調整できるようにしたい。

別のデリゲート「SpeedSliderDelegate」をデリゲートしてリッスンする Deck クラスがあります。Deck クラスが SpeedSliderDelegate からの変更をリッスンし、値を更新するようにします。

SpeedSliderDelegate をリッスンするように Deck.m モデルを設定するにはどうすればよいですか... (if(self = [super init] in Deck.m の直後)

SpeedSliderDelegate.h

@protocol SpeedSliderDelegate <NSObject>
-(void)setSpeed:(float)currentSpeed;
@end

Deck.h

#import "SpeedSliderDelegate.h"
#import <Foundation/Foundation.h>
#import "Card.h"

@protocol DeckLooperDelegate <NSObject>
-(void)cardHasChangedTo:card1 aCard2:(Card *)card2 totalCount:(NSInteger)totalCount stopTimer:(NSTimer*)stopTimer cards:(NSArray *)cards;
@end

@interface Deck : NSObject <SpeedSliderDelegate>

@property (nonatomic, retain)NSMutableArray *cards;
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)id <DeckLooperDelegate> delegate;
@property (nonatomic)NSInteger total;
@property (nonatomic) float testSpeed;

- (NSInteger) cardsRemaining;
- (void) startTimerLoop;

@end

Deck.m

#import "Deck.h"
#import "Card.h"

@implementation Deck

@synthesize cards;
@synthesize timer;
@synthesize delegate;
@synthesize total, testSpeed;
- (id) init
{
    if(self = [super init])
    {
        //self.delegate = self    something like this... 

        cards = [[NSMutableArray alloc] init];
        NSInteger aCount, picNum = 0;

        for(int suit = 0; suit < 4; suit++)
        {
            for(int face = 1; face < 14; face++, picNum++)
            {
                if (face > 1 && face < 7)
                    aCount = 1;
                else if (face > 6 && face < 10)
                    aCount = 0;
                else
                    aCount = -1;


                NSString* imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"card_%d", picNum] ofType:@"png"];

                UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

                Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
                                                countValue:(NSInteger)aCount
                                                suit:(Suit)suit
                                                cardImage:(UIImage *)theImage];

                [cards addObject:card];
            }
        }
    }
    return self;

}

-(void)setSpeed:(float)currentSpeed
{
    testSpeed = currentSpeed;
}

-(void)startTimerLoop
{

    if (!timer)
    {
        timer = [NSTimer scheduledTimerWithTimeInterval:testSpeed target:self
                    selector:@selector(timerEvent:) userInfo:nil repeats:YES ];
        NSLog(@"Timer started!!!");
    }
}

-(void)timerEvent:(NSTimer*)timer
{
    srand(time(nil));

    int index = rand()%[cards count];
    Card *randomCard =[cards objectAtIndex:index];
    [cards removeObjectAtIndex:index];
    NSLog(@"%@" ,randomCard);

    int index1 = rand()%[cards count];
    Card *randomCard1 =[cards objectAtIndex:index1];
    [cards removeObjectAtIndex:index1];
    NSLog(@"%@" ,randomCard1);

    total += (randomCard.countValue + randomCard1.countValue);

    [self.delegate cardHasChangedTo:randomCard aCard2:randomCard1 totalCount:total stopTimer:self.timer cards:cards];

    if ([cards count] == 0)
    {
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (NSInteger) cardsRemaining
{
    return [cards count];
}

- (NSString *) description
{
    NSString *desc = [NSString stringWithFormat:@"Deck with %d cards\n",
                        [self cardsRemaining]];
    return desc;
}

@end

GameViewController.h

#import "Deck.h"
#import "SpeedSliderDelegate.h"

@interface GameViewController : UIViewController <DeckLooperDelegate>

@property (nonatomic, retain) IBOutlet UIImageView *cardDisplay, *cardDisplay1;
@property (weak, nonatomic) IBOutlet UILabel *cardName, *cardName1;
@property (weak, nonatomic) IBOutlet UILabel *cardCount, *cardCount1;
@property (weak, nonatomic) IBOutlet UILabel *totalCount;
@property (nonatomic)int stop1;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;
@property (weak, nonatomic) IBOutlet UIButton *restartButton;
@property (weak, nonatomic) IBOutlet UIButton *homeButton;
@property (weak, nonatomic) IBOutlet UISlider *speed;
@property (nonatomic, retain) id <SpeedSliderDelegate> delegate;


- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
- (IBAction)hideShow:(id)sender;
- (IBAction)homeAction:(id)sender;

@end

GameViewController.m

#import <QuartzCore/QuartzCore.h>
#import "GameViewController.h"
#import "Deck.h"

@implementation GameViewController

@synthesize cardDisplay, cardDisplay1, cardName, cardName1, cardCount, cardCount1, totalCount, stop1, stopButton, restartButton, homeButton, speed, delegate;

- (void)viewDidLoad
{
    [super viewDidLoad];

    Deck *deck = [[Deck alloc]init];
    NSLog(@"%@", deck);

    for (id cards in deck.cards)
    {
         NSLog(@"%@", cards);
    }

    deck.delegate = self;
    [deck startTimerLoop];

    cardDisplay.layer.cornerRadius = 7;
    cardDisplay.clipsToBounds = YES;

    cardDisplay1.layer.cornerRadius = 7;
    cardDisplay1.clipsToBounds = YES;

    [restartButton setHidden:YES];

    [delegate setSpeed:speed.value];

}


//#pragma mark - DeckDelegate
-(void)cardHasChangedTo:(Card *)card1 aCard2:(Card *)card2 totalCount:(NSInteger)totalC stopTimer:(NSTimer *)stopTimer cards:(NSArray *)cards
{

    [self.cardDisplay setImage:card1.cardImage];
    [self.cardDisplay1 setImage:card2.cardImage];

    self.cardName.text = card1.description;
    self.cardName1.text = card2.description;

    self.cardCount.text = [NSString stringWithFormat:@"%d", card1.countValue];
    self.cardCount1.text = [NSString stringWithFormat:@"%d", card2.countValue];

    self.totalCount.text = [NSString stringWithFormat:@"%d", totalC];

    if (stop1 == 86)
    {
        [stopTimer invalidate];
        stopTimer = nil;
    }

    if ([cards count] == 0)
    {
        [restartButton setHidden:NO];
    }

}

- (IBAction)start:(id)sender
{
    stop1 = 85;

    [cardDisplay setHidden:YES];
    [cardDisplay1 setHidden:YES];

    self.totalCount.text = @"0";

    self.cardCount.text = @"0";
    self.cardCount1.text = @"0";    

    self.cardName.text = @"";
    self.cardName1.text = @"";

    Deck *deck = [[Deck alloc]init];

    [NSTimer scheduledTimerWithTimeInterval:2 target:self
        selector:@selector(cardDisplayDelay:) userInfo:nil repeats:NO];

    NSLog(@"%@", deck);

    deck.delegate = self;
    [deck startTimerLoop];

    cardDisplay.layer.cornerRadius = 7;
    cardDisplay.clipsToBounds = YES;

    cardDisplay1.layer.cornerRadius = 7;
    cardDisplay1.clipsToBounds = YES;

    [restartButton setHidden:YES];

}

- (IBAction)stop:(id)sender
{
    stop1 = 86;  //cancelled

    [NSTimer scheduledTimerWithTimeInterval:2 target:self
                selector:@selector(restartButtonDelay:) userInfo:nil repeats:NO];
}

-(void)restartButtonDelay:(NSTimer*)timer
{
    [restartButton setHidden:NO];
}

-(void)cardDisplayDelay:(NSTimer*)timer
{
    [cardDisplay setHidden:NO];
    [cardDisplay1 setHidden:NO];
}


- (IBAction)hideShow:(id)sender
{
    if ([cardCount isHidden])
    {
        [cardCount setHidden:NO];
        [cardCount1 setHidden:NO];
        [totalCount setHidden:NO];
    }

    else
    {
        [cardCount setHidden:YES];
        [cardCount1 setHidden:YES];
        [totalCount setHidden:YES];
    }
}

- (IBAction)homeAction:(id)sender
{
    stop1 = 86;  //cancelled
}


@end
4

2 に答える 2

0

デッキはそれ自身の代表ではありません。今、あなたのコードは、デッキがSpeedSliderDelegateであり、デッキのデリゲートがDeckLooperDelegateであることを示しています。

常にデッキのデリゲートが必要な場合は、次のように行うことができます。

- (id) initWithDelegate:(id<DeckLooperDelegate>)delegate {
    if (self = [super init]) {
        self.delegate = delegate;

        cards = [[NSMutableArray alloc] init];
        NSInteger aCount, picNum = 0;

        // etc etc etc
    }
}

次に、デッキを作成するDeckLooperDelegateで、次のように呼び出します。

Deck *deck = [[Deck alloc] initWithDelegate:self];

次に、スピードスライダーでデッキをデリゲートとして設定します。

mySpeedSlider.delegate = deck;

これで、mySpeedSliderは[delegate setSpeed:]を呼び出して、デッキの速度を変更できます。

于 2013-01-07T22:50:55.483 に答える
0

更新中のクラス .h

@protocol DoSomethingDelegate <NSObject>
-(void)doSomething;
@end

@property (assign, nonatomic) id <DoSomethingDelegate> delegate;

更新中のクラス .m

-(void)doSomethingSomewhereElse {
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(doSomething)]) {
           [self.delegate performSelector:@selector(doSomething)];
       }
    } else {
       NSLog(@"Delgate doesn't implement doSomething");
    }
}

受信クラスの .h: プロトコルに準拠<DoSomethingDelegate>

@interface myDoSomethingClass : NSObject <DoSomethingDelegate>
...

受信クラスの .m にデリゲート メソッドを実装します。

-(void)someInit{
    //set the delegate to self
}

ビューコントローラーで.m

-(void)doSomething{
     NSLog(@"The delegate told me to do this!");
}
于 2013-01-07T21:12:53.217 に答える