2016-08-09 18 views
0

こんにちは、私は客観的なCで新しい、今私は"What's the word"のようなアプリを作成しようとします。私はこれを見つけるtutorialと同様にそれをlernedすることができます。しかし、私はいくつかの問題があります。私はボタンをクリックすると、現在のタイトルがplacesViewのlableを置き換えたいと思う。 「displayChar」という名前のLettersView.mのボタンクリックメソッド。私はcurrentTitleを取得することに成功しましたが、今はGameControllerに渡して「場所」にテキストを貼り付ける方法がわかりません。 私はどんな助けにも感謝します!ここでボタンからUILabelにテキストを設定する方法別のビューでクリックする

は私のコード

LettersView.h

#import <UIKit/UIKit.h> 
@class LettersView; 
@protocol LetterClickDelegateProtocol <NSObject> 
-(void)letterView:(LettersView*)letterView addChar:(NSString *)addChar; 
@end 

@interface LettersView : UIImageView 

@property (strong, nonatomic, readonly) NSString* letter; 
@property (assign, nonatomic) BOOL isMatched; 
@property (strong, nonatomic) NSString *clickLetter; 

@property (weak, nonatomic) id<LetterClickDelegateProtocol> clickDelegate; 

@property (strong, nonatomic) UIButton *lblChar; 

-(instancetype)initWithLetter:(NSString*)letter andSideLength:(float)sideLength; 

@end 

LettersView.m

#import "LettersView.h" 
#import "config.h" 

@implementation LettersView{ 
    NSInteger _xOffset, _yOffset; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    NSAssert(NO, @"Use initWithLetter:andSideLength instead"); 
    return nil; 
} 

-(instancetype)initWithLetter:(NSString*)letter andSideLength:(float)sideLength 
{ 
    //the letter background 
    UIImage* img = [UIImage imageNamed:@"[email protected]"]; 

    //create a new object 
    self = [super initWithImage:img]; 

    if (self != nil) { 

     //resize the letters 
     float scale = sideLength/img.size.width; 
     self.frame = CGRectMake(0,0,img.size.width*scale, img.size.height*scale); 

     UIButton *lblChar = [[UIButton alloc] initWithFrame:self.bounds]; 
     lblChar.tintColor = [UIColor blackColor]; 
     lblChar.backgroundColor = [UIColor clearColor]; 
     [lblChar setTitle:letter forState:UIControlStateNormal]; 
     [lblChar addTarget:self action:@selector(displaychar:)forControlEvents:UIControlEventTouchUpInside]; 
     [self addSubview:lblChar]; 

     self.isMatched = NO; 

     _letter = letter; 
     self.userInteractionEnabled = YES; 
    } 
    return self; 
} 

-(void)displayChar:(id)sender { 
    UIButton *lblChar = (UIButton *)sender; 
    NSLog(@" The button's title is %@.", lblChar.currentTitle); 
    _clickLetter = lblChar.currentTitle; 
    if (self.clickDelegate) { 
     [self.clickDelegate letterView:self addChar:lblChar.currentTitle]; 
    } 
    NSLog(@"hu %@", _clickLetter); 
} 

PlacesView.h

です PlacesViewへの参照を保持するために
// PlacesView.m 
#import "PlacesView.h" 
#import "config.h" 

@implementation PlacesView 

-(id)initWithFrame:(CGRect)frame { 
    NSAssert(NO, @"Use initwithletter"); 
    return nil; 
} 
-(instancetype)initWithLetter:(NSString *)letter andSideLength:(float)sideLength 
{ 
    UIImage *img = [UIImage imageNamed:@"[email protected]"]; 
    self = [super initWithImage: img]; 

    if (self != nil) { 
     self.isMatched = NO; 

     float scale = sideLength/img.size.width; 
     self.frame = CGRectMake(0, 0, img.size.width*scale, img.size.height*scale); 

     //bullshit time 
     _fieldForLetter = [[UILabel alloc] initWithFrame:self.bounds]; 
     _fieldForLetter.textAlignment = NSTextAlignmentCenter; 
     _fieldForLetter.textColor = [UIColor blackColor]; 
     _fieldForLetter.backgroundColor = [UIColor clearColor]; 
     _fieldForLetter.text = @"*"; // if button pressed button title placed here. 
     [self addSubview:_fieldForLetter]; 
     _letter = letter; 
    } 
    return self; 
} 
@end 

GameController.m

#import "GameController.h" 
#import "config.h" 
#import "LettersView.h" 
#import "PlacesView.h" 
#import "AppDelegate.h" 

@implementation GameController { 

    //tile lists 
    NSMutableArray* _letters; 
    NSMutableArray* _places; 

} 

-(instancetype)init { 
    self = [super init]; 
    if (self != nil) { 
     self.points = [[PointsController alloc] init]; 
     self.audioController = [[AudioController alloc] init]; 
     [self.audioController preloadAudioEffects: kAudioEffectFiles]; 

    } 

    return self; 
} 


-(void)dealRandomWord { 

    NSAssert(self.level.words, @"Level not loaded"); 

    // random word from plist 
    NSInteger randomIndex = arc4random()%[self.level.words count]; 
    NSArray* anaPair = self.level.words[ randomIndex ]; 


    NSString* word1 = anaPair[1]; // answer 
    NSString* word2 = anaPair[2]; // some letters 
    _helpstr = anaPair[3]; // helper 

    NSLog(@"qweqweq %@ %@" , word1 , word2); 

    NSInteger word1len = [word1 length]; 
    NSInteger word2len = [word2 length]; 

    NSLog(@"phrase1[%li]: %@", (long)word1len, word1); 
    NSLog(@"phrase2[%li]: %@", (long)word2len, word2); 

    //calculate the letter size 
    float letterSide = ceilf(kScreenWidth*0.9/(float)MAX(word1len, word2len)) - kTileMargin; 

    //get the left margin for first letter 
    float xOffset = (kScreenWidth - MAX(word1len, word2len) * (letterSide + kTileMargin))/2; 

    //adjust for letter center 
    xOffset += letterSide/2; 
    float yOffset = 1.5* letterSide; 


// init places list 
    _places = [NSMutableArray arrayWithCapacity: word1len]; 

// create places 
    for (NSInteger i = 0; i<word1len; i++){ 
     NSString *letter = [word1 substringWithRange:NSMakeRange(i, 1)]; 


     if (![letter isEqualToString:@" "]) { 
      PlacesView* place = [[PlacesView alloc] initWithLetter:letter andSideLength:letterSide]; 
      place.center = CGPointMake(xOffset + i*(letterSide + kTileMargin), kScreenHeight/4); 


      [self.gameView addSubview:place]; 
      [_places addObject: place]; 
     } 
    } 

    //init letters list 
    _letters = [NSMutableArray arrayWithCapacity: word2len]; 

    //create letter 
    for (NSInteger i=0;i<word2len;i++) { 
     NSString* letter = [word2 substringWithRange:NSMakeRange(i, 1)]; 


     if (![letter isEqualToString:@" "]) { 
      LettersView* letv = [[LettersView alloc] initWithLetter:letter andSideLength:letterSide]; 
      letv.center = CGPointMake(xOffset + i * (letterSide + kTileMargin), kScreenHeight); // "/3*4" 
      if (i > 6) { 
       letv.center = CGPointMake(-5.15 * xOffset + i * (letterSide + kTileMargin), kScreenHeight + yOffset); // "/3*4" 
      } 
      letv.clickDelegate = self; 

      [self.gameView addSubview:letv]; 
      [_letters addObject: letter]; 
     } 
    } 

} 

-(void)letterView:(LettersView *)letterView addChar:(NSString *)addChar 
{ 
    PlacesView* placesView = nil; 

    for (PlacesView* pl in _places) { 
     //if (CGRectContainsPoint(pl.frame, pt)) { 
     if() { 
      //placesView = pl; 
      placesView.fieldForLetter.text = letterView.lblChar.currentTitle; 

      break; 
     } 
    } 
    //1 check if target was found 
    if (placesView!=nil) { 

     //2 check if letter matches 
     if ([placesView.letter isEqualToString: letterView.letter]) { 
      [self placeLetter:letterView atTarget:placesView]; 
      [self.audioController playEffect: kSoundLetterTap]; 

      self.points.points += self.level.coinsPerLvl; //ne nado tak 

      NSLog(@"Current points %d" , self.points.points); 

      [self checkForSuccess]; 
     } else { 
      [self.audioController playEffect:kSoundFail]; 
      [self addAlert:@"ne success" andMessage:@"You lose!" andButton:@"eshe cyka"]; 
     } 
    } 
} 
-(void)placeLetter:(LettersView*)letterView atTarget:(PlacesView*)placeView { 
    placeView.isMatched = YES; 
    letterView.isMatched = YES; 

    letterView.userInteractionEnabled = NO; 
} 

-(void)checkForSuccess { 
    for (PlacesView* p in _places) { 
     //no success, bail out 
     if (p.isMatched==NO) return; 
    } 
    NSLog(@"ya!"); 
    [self addAlert:@"Success" andMessage:@"You win!" andButton:@"eshe cyka"]; 
    [self.audioController playEffect:kSoundSuccess]; 
} 

-(void)addAlert: (NSString *)addTitle andMessage: (NSString *)alertMessage andButton: (NSString *)alertButton { 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 

     window.rootViewController = [UIViewController new]; 
     window.windowLevel = UIWindowLevelAlert + 1; 
     UIAlertController *alert = [UIAlertController alertControllerWithTitle: addTitle message:alertMessage preferredStyle:UIAlertControllerStyleAlert]; 
     UIAlertAction *defaultAction= [UIAlertAction actionWithTitle:alertButton style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

      window.hidden = YES; 

     }]; 
     [alert addAction:defaultAction]; 
     [window makeKeyAndVisible]; 
     [window.rootViewController presentViewController:alert animated:YES completion:nil]; 
     }); 
} 
@end 

答えて

0

あなたGameControllerニーズ。また、LettersViewにアクションを割り当てることができます。LettersViewのボタンを押すと、GameControllerはそれを取り出してPlacesViewでアクションを実行します。 GameControllerは、他の両方のクラスが共通して持つものなので、それらの間のアクションを処理できます。

もう1つの方法は、NSNotificationCenterを使用し、ボタンを押したときにLettersViewにメッセージを投稿し、PlacesViewでそれを聞くことです。

さらに別の方法ではdelegatesを使用しています。GameControllerは、PlacesViewがデリゲートとして設定されていることを確認しています。 LettersViewのボタンを押すと、PlacesViewが聴くデリゲートメソッドが呼び出されます。

私は最初のオプションを使用します。

+0

助けてくれてありがとうございました。 –

関連する問題