2014-02-19 116 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連接到textFields和按鈕。 –

+0

有一個「完成」按鈕 - 當你在鍵盤上按下這個按鈕時,你想讓鍵盤消失嗎? – Rich

回答

5

您可以添加到UITapGestureRecognizerself.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

這是正確的,但您也可以在筆尖編輯器中添加手勢識別器。無需打字!只需將Tap Gesture Recognizer拖動到視圖中,然後選擇手勢識別器,轉到連接檢查器,然後從Received Actions拖動到視圖控制器並進行IBAction連接。 – jsd

+0

@MaurícioGiordano我仍然會讓鍵盤上的「完成」按鈕關閉鍵盤! :) – Rich

3

只需在身份檢查員中將視圖的類從UIView更改爲UIControl,然後就可以連接IBActions