2014-02-19 4 views
0

Я новичок в iOS, и я следую this tutorial.Невозможно подключить IBAction для просмотра

Вот скриншот от меня, пытающегося подключить IBAction к моему представлению.

Я хочу выполнить метод releaseKeyboard всякий раз, когда я касаюсь вида (т. Е. Закрываю клавиатуру).

Я не использую раскадровку.

screenshot

Мои файлы:

  • challAppDelegate.h
  • challAppDelegate.m
  • challViewController.h
  • challViewController.m
  • challViewController.xib

challAppDelegate.h

#import <UIKit/UIKit.h> 

@interface challAppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    UINavigationController *navigationController; 
} 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) UINavigationController *navigationController; 

@end 

challAppDelegate.m

#import "challAppDelegate.h" 
#import "challViewController.h" 

@implementation challAppDelegate 

@synthesize window = _window; 
@synthesize navigationController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UIViewController *rootController = 
    [[challViewController alloc] 
    initWithNibName:@"challViewController" bundle:nil]; 

    navigationController = [[UINavigationController alloc] 
          initWithRootViewController:rootController]; 

    self.window = [[UIWindow alloc] 
        initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [self.window addSubview:navigationController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 

} 
... 
... 

challViewController.h

#import <UIKit/UIKit.h> 

@interface challViewController : UIViewController 

@property(nonatomic, retain) IBOutlet UITextField *signInEmailAddress; 
@property(nonatomic, retain) IBOutlet UITextField *signInPassword; 

@property(nonatomic, retain) IBOutlet UIButton *signInSignInButton; 
@property(nonatomic, retain) IBOutlet UIButton *signInRegisterButton; 

-(void) releaseKeyboardAction; 

-(IBAction) signInAction:(int)sender; 

-(IBAction) registerAction:(int)sender; 

-(IBAction) releaseKeyboard:(id)sender; 

@end 

challViewController.m

#import "challViewController.h" 

@interface challViewController() 

@end 

@implementation challViewController 

@synthesize signInEmailAddress; // cria os getters e setters 
@synthesize signInPassword; // cria os getters e setters 

@synthesize signInSignInButton; // cria os getters e setters 
@synthesize signInRegisterButton; // cria os getters e setters 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    self.title = @"Sign In"; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)releaseKeyboardAction 
{ 
    [signInEmailAddress resignFirstResponder]; 
    [signInPassword resignFirstResponder]; 
} 

- (IBAction)releaseKeyboard:(id)sender 
{ 
    [self releaseKeyboardAction]; 
} 

- (IBAction)registerAction:(int)sender 
{ 
    // 
} 

- (IBAction)signInAction:(int)sender 
{ 
    // 
} 

@end 

Что я делаю неправильно?

Благодаря

+1

Вы не можете подключить 'IBAction' как' IBOutlet' для вашего вида. Что вы хотите сделать с представлением и методом 'releaseKeyboard:'? – Rich

+0

Чтобы отпустить клавиатуру, когда вы нажимаете в любом месте просмотра (поскольку на клавиатуре нет кнопки выхода). Я подключил IBActions к текстовым полям и кнопкам. –

+0

Есть кнопка «Готово» - когда вы нажимаете эту кнопку на клавиатуре, вы хотите, чтобы клавиатура исчезла? – Rich

ответ

5

Вы можете добавить UITapGestureRecognizer к self.view.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    self.title = @"Sign In"; 

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(releaseKeyboardAction)]; 
    [self.view addGestureRecognizer:gesture]; 
} 

Цель этого жеста распознавани указывает на ваш метод releaseKeyboardAction.

+0

Это сработало! Благодаря! –

+1

Это правильно, но вы также можете добавить распознаватель жестов в редакторе nib. Не требуется ввод текста! Просто перетащите указатель жесты на изображение, затем выберите распознаватель жестов, перейдите к инспектору соединений и перетащите из принятых действий в контроллер вида и сделайте соединение IBAction. – jsd

+0

@ MaurícioGiordano Я все равно сделаю кнопку «Готово» на клавиатуре, даже если вы отпустите клавиатуру! :) – Rich

3

Просто измените класс вида от UIView до UIControl в инспекторе идентификации, а затем вы можете подключиться IBActions.

Смежные вопросы