我有一个复选按钮和3个视图,一个TextField
我希望检查按钮能够知道它所在的视图,
view1 view1或view 3.这样当用户输入正确的答案时,特定的操作仅适用于该视图!
- (IBAction)btncheck:(id)sender {
// Enter has been pressed, see if the text in the textView equals our string
NSString *answer = [_textbox.text stringByReplacingOccurrencesOfString:@"q telecom " withString:@""];
// to block user from getting incorrect answer with no text in text field
if([answer isEqualToString:@""]){
}
else
if ([answer isEqualToString:@"q telecom"]) {
//Name:Q Telecom - FIRST TEXTFIELD SAVED - This will save the answer (q telecom forever to the user) *SAVED
NSString *savestring = _textbox.text; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:@"savedstring"];
[defaults synchronize];
// String is correct, resign keyboard, xclear, and protection/shield
_keyboard.hidden = YES;
_textXclear.hidden = YES;
_protectionOutBtn.enabled = NO;
//Perfect button
[_closeone setHidden:NO];
[_wrongone setHidden:YES];
score = MAX (score +100, 1);
[scoreLabel setText:[NSString stringWithFormat:@"Σκορ: %d", score]];
// remeber that coins must always be coins = coins + numeber to add up correctly
coins = coins +5;
if(score == 100) {coins = coins + 3;}
[coinsLabel setText:[NSString stringWithFormat:@"%d", coins]];
//coin animation start
FImageWinfive.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"coindust2.png"],[UIImage imageNamed:@"coindust3.png"],[UIImage imageNamed:@"coindust4.png"],[UIImage imageNamed:@"coindust5.png.png"],[UIImage imageNamed:@"coindust6.png"],[UIImage imageNamed:@"coindust7.png"],[UIImage imageNamed:@"coindust8.png"],[UIImage imageNamed:@"coindust9.png"],[UIImage imageNamed:@"coindust10.png"],[UIImage imageNamed:@"coindust11.png"],[UIImage imageNamed:@"coindust12.png"], nil];
[FImageWinfive setAnimationRepeatCount:1];
FImageWinfive.animationDuration = 1.5;
FImageWinfive.animationRepeatCount = 1;
[FImageWinfive startAnimating];
//coin animation finished
_textbox.enabled = NO;
}
else {
CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
[shake setDuration:0.1];
[shake setRepeatCount:2];
[shake setAutoreverses:YES];
[shake setFromValue:[NSValue valueWithCGPoint:
CGPointMake(shaker.center.x - 5,shaker.center.y)]];
[shake setToValue:[NSValue valueWithCGPoint:
CGPointMake(shaker.center.x + 5, shaker.center.y)]];
[shaker.layer addAnimation:shake forKey:@"position"];
// Was not correct. Notify user, or just don't do anything
[_wrongone setHidden:NO];
score = score -3;
[scoreLabel setText:[NSString stringWithFormat:@"Σκορ: %d", score]];
//SCORE
}
if (score < 100) {
closeonechange.text = @"Σωστά!";
_imageCorP.image = [UIImage imageNamed:@"boxg_25.png"];
} else {
closeonechange.text = @"Αψογα!";
_imageCorP.image = [UIImage imageNamed:@"telioo.png"];
}
答案 0 :(得分:1)
可能有一种更简单的方法,但这是一种选择。
首先,在viewController
的{{1}}文件中,.m
:
@property
现在,假设这个视图控制器委派了所有这三个文本字段:
@interface SomeViewController()
@property (nonatomic, weak) UITextField *activeField;
@end
现在在处理按钮触摸的方法中:
- (void)textFieldDidBeginEditing:(UITextField*)textField {
self.activeField = textField;
}
稍微更好,更清晰的解决方案是使用- (IBAction)btncheck:(id)sender {
if (self.lastActiveField == self.view1) {
// check view 1
} else if (self.lastActiveField == self.view2) {
// check view 2
} else if (self.lastActiveField == self.view3) {
// check view 3
} else {
// either no field has been selected yet or there are other text fields
// delegated by this view controller
}
}
并使用此enum
设置文本字段的标记,然后在enum
中,启用{ {1}}。
例如:
IBAction
跟踪上一个活动按钮的属性:
enum
现在,您可以在故事板或视图控制器的生命周期方法中设置它:
typedef NS_ENUM(NSInteger, ActiveFieldEnum) {
tag_VIEW1 = 100,
tag_VIEW2 = 101,
tag_VIEW3 = 102
};
文本字段委托:
@property (nonatomic,assign) ActiveFieldEnum lastActiveField;
现在按钮方法:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// set up your view
self.view1.tag = tag_VIEW1;
self.view2.tag = tag_VIEW2;
self.view3.tag = tag_VIEW3;
}