2016-08-09 127 views
0

大家好我新的目標c,現在我嘗試創建像"What's the word"這樣的應用程序。我發現這個tutorial,並盡我所能地使用它。但我有一些問題。我想當我點擊按鈕currentTitle替換placesView中的標籤。 LettersView.m中的按鈕點擊方法命名爲「displayChar」。我在獲得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.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需要保持一個參考PlacesView。它也可以將動作分配到LettersView,因此當按下LettersView中的按鈕時,GameController將取回該動作並在PlacesView中執行動作。 GameController是其他類共有的,因此它可以處理它們之間的任何操作。

另一種選擇是使用NSNotificationCenter並在按下按鈕時在LettersView中發佈消息,並在PlacesView中監聽它。

另一種方法是使用delegates其中GameController確保將PlacesView設置爲委託。當按下LettersView的按鈕時,它將調用PlacesView監聽的委託方法。

我會選擇第一個選項。

+0

感謝您的建議,對我來說非常有用! –

相關問題