大家好,我是Objective c和iOS dev的新手。现在我尝试创建一个类似"What's the word"的应用 我有按钮和地方的问题。当我第一次点击带有Letter的按钮时一切都好([图1],[图2]),但是第二次按钮试着去第一个地方,但它不起作用([图3]) 。我希望每次下一次单击按下下一个按钮(或传递currentTitle)。我尝试替换按钮并仅使用currentTitle,结果是(pic 4)。 我认为(但不确定)
中的问题- (void)letterView :( LettersView *)letterView didClick:(BOOL)didClick addChar:(NSString *)addChar
但我不知道如何解决它。我很感激任何帮助。
P.S我在评论中附上所有图像。
我的代码:
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/2);
[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/4*3); // "/3*4"
if (i > 6) {
letv.center = CGPointMake(-5.15 * xOffset + i * (letterSide + kTileMargin), kScreenHeight/4*3 + yOffset); // "/3*4"
}
letv.clickDelegate = self;
[self.gameView addSubview:letv];
[_letters addObject: letter];
}
}
}
-(void)letterView:(LettersView *)letterView didClick:(BOOL)didClick addChar:(NSString *)addChar
{
PlacesView* placesView = nil;
for (PlacesView* pl in _places) {
///if (didClick) {
placesView = pl;
placesView.fieldForLetter.text = [placesView.fieldForLetter.text stringByAppendingString:letterView.clickLetter];
// didClick = NO;
NSLog(@"MAIN LOG %@", placesView.fieldForLetter.text);
break;
//}
}
// check if place was found
if (placesView!=nil) {
// 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:@"ok"];
}
}
}
-(void)placeLetter:(LettersView*)letterView atTarget:(PlacesView*)placeView {
placeView.isMatched = YES;
letterView.isMatched = YES;
letterView.userInteractionEnabled = NO;
// [UIView animateWithDuration:0.35
// delay:0.00
// options:UIViewAnimationOptionCurveEaseOut
//
// animations:^{
// letterView.center = placeView.center;
// letterView.transform = CGAffineTransformIdentity;
// }
//
// completion:^(BOOL finished){
// placeView.hidden = YES;
// }];
}
-(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
LetterView.m
#import "LettersView.h"
#import "config.h"
#import "PlacesView.h"
@implementation LettersView
- (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:@"btn_letter@2x.png"];
//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);
//more init
// button right way!!!!!!!!
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(displayСhar:)forControlEvents:UIControlEventTouchUpInside];
[self addSubview:lblChar];
self.isMatched = NO;
_letter = letter;
self.userInteractionEnabled = YES;
}
return self;
}
// !!!!right way
-(void)displayСhar:(id)sender {
UIButton *lblChar = (UIButton *)sender;
NSLog(@" The buttons title is %@", lblChar.currentTitle);
_clickLetter = lblChar.currentTitle;
BOOL didClick = YES;
if (self.clickDelegate) {
[self.clickDelegate letterView:self didClick:(BOOL)didClick addChar:_clickLetter];
}
NSLog(@"CLICKLETTER %@", _clickLetter);
}
@end
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:@"btn_input@2x.png"];
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;
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTaped:)];
[_fieldForLetter addGestureRecognizer:tapGesture];
}
return self;
}
-(void) didTaped:(UITapGestureRecognizer *)tapGesture{
if( [tapGesture state] == UIGestureRecognizerStateEnded ) {
// the label that was tapped
UILabel* fieldForLetter = (UILabel*)[tapGesture view];
// do things with your label
NSLog(@"SOME ** IN CONSOLE: %@", fieldForLetter);
}
}
@end
答案 0 :(得分:0)
如果你不这样做,我建议使用一组Ui对象,比如
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btnCollection;