나는 여러 시간 동안이 문제를 해결하려고 노력 해왔다. 내가 찾은 게시물 중 어느 것도 해결책을 제공하지 못했습니다. 문제는 textField가 하위보기에 있고 키보드를 숨기려면 Return 단추에 응답하는 방법을 알아낼 수 없다는 것입니다. 다음은 내 View Controller입니다.- (BOOL) textFieldShouldReturn : (UITextField *) textField; 키보드를 하위보기로 옮기려면
#import <UIKit/UIKit.h>
#import "Combatant.h"
@interface ViewController : UIViewController <CombatantDelegate>
- (IBAction) addView;
- (IBAction) roll;
@end
#import "ViewController.h"
static int startY = 60;
@interface ViewController()
{
NSMutableArray * customViews;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
customViews = [[NSMutableArray alloc] init];
}
- (IBAction) addView
{
Combatant * thing = [[NSBundle mainBundle] loadNibNamed:@"Combatant" owner:nil options:nil][0];
[self.view addSubview: thing];
thing.center = CGPointMake(self.view.center.x, startY + customViews.count * thing.bounds.size.height);
thing.combatDelegate = self;
[customViews addObject:thing];
}
- (IBAction) roll
{
for (Combatant * cust in customViews)
{
[cust rollWasTapped];
}
}
@end
여기 내 맞춤 클래스입니다.
#import <UIKit/UIKit.h>
@class Combatant;
@protocol CombatantDelegate <NSObject>
@end
@interface Combatant : UIView <UITextFieldDelegate>
{
IBOutlet UITextField * name;
IBOutlet UITextField * base;
IBOutlet UITextField * die;
IBOutlet UITextField * mod;
IBOutlet UILabel * total;
NSString *value;
int dRoll;
int cBase;
int cMod;
}
@property (nonatomic, strong, readwrite) NSString *value;
@property (nonatomic, strong) IBOutlet UITextField * name;
@property (nonatomic, strong) IBOutlet UITextField * base;
@property (nonatomic, strong) IBOutlet UITextField * die;
@property (nonatomic, strong) IBOutlet UITextField * mod;
@property (nonatomic) IBOutlet UILabel * total;
-(void) rollWasTapped;
-(BOOL) textFieldShouldReturn:(UITextField *)textField;
@property (nonatomic, weak) id<CombatantDelegate> combatDelegate;
-(int) dieRoll;
-(int) convertBase;
-(int) convertMod;
@end
#import "Combatant.h"
@implementation Combatant
@synthesize value;
@synthesize name;
@synthesize base;
@synthesize die;
@synthesize mod;
@synthesize total;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[name resignFirstResponder];
return YES;
}
- (void) rollWasTapped;
{
int tot;
tot = [self convertBase] + [self dieRoll] + [self convertMod];
total.text = [NSString stringWithFormat:@"%i", tot];
NSLog(@" totaling %i ", tot);
}
-(int) dieRoll
{
int val = [die.text intValue];
if (val <7 && val >0)
{
NSLog(@" %i die", val);
} else {
NSLog(@" there are no dice");
val = 0; }
int d = 0;
for (int i = 0; i < val; i++) {
d = d + arc4random()%6 + 1;
}
dRoll = d;
NSLog(@"%i die roll result = %i ", val, dRoll);
return dRoll;
}
-(int) convertBase
{
cBase = [base.text intValue];
NSLog(@"base = %i", cBase);
if (cBase > -1 && cBase < 20)
{
return cBase;
}
return 0;
}
-(int) convertMod
{
cMod = [mod.text intValue];
NSLog(@"mod = %i", cMod);
if (cMod > -20 && cMod < 20)
{
return cMod;
}
return 0;
}
- (void) dealloc
{
self.combatDelegate = nil;
}
@end
코드는 키보드 숨기기를 제외한 모든 측면에서 완벽하게 작동합니다.
name.delegate = self;
또는 당신은 당신의 뷰 컨트롤러에 위임 콘센트를 드래그하여, 연결 Inspector에서 스토리 보드에서이 작업을 수행 할 수 있습니다 :
텍스트 필드가 모두 제대로 작동하고 응용 프로그램이 충돌없이 오류없이 경고없이 모든 데이터를 완벽하게 처리합니다. 키보드를 없애기위한 방법 (K 명령어 제외)을 찾을 수 없습니다. 이것은 결국 장치에있게 될 것이므로 명령 K는 쓸모가 없습니다. textField는 뷰 컨트롤러의 버튼에 의해 동적으로 생성됩니다. 이것은 나에게 계층 구조 문제라고 생각됩니다. 이 응용 프로그램의 다른 모든 측면은 계층 노래와 춤을 잘 않습니다. – Hammerhead96
하위 뷰 xib에서 대리자가 뷰에 연결되지 않았습니다. 첫 번째 응답자에 대한 호출에서도 "이름"은 더 이상 표시되지 않으며 [textField resignFirstResponder]; ... 이제 작동합니다. 문제 해결됨. 모두 감사합니다! – Hammerhead96