【问题标题】:Can't connect IBAction to view无法连接 IBAction 以查看
【发布时间】:2014-02-20 07:15:39
【问题描述】:

我是 iOS 新手,我正在关注 this tutorial

这是我尝试将 IBAction 连接到我的视图的屏幕截图。

每当我触摸视图(即关闭键盘)时,我都想执行 releaseKeyboard 方法。

我没有使用故事板。

我的文件:

  • 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

我做错了什么?

谢谢

【问题讨论】:

  • 您无法将IBAction 连接为IBOutlet 以供您查看。你想用视图和releaseKeyboard: 方法做什么?
  • 当您按下视图中的任意位置时释放键盘(因为键盘没有退出按钮)。我确实将 IBActions 连接到 textFields 和按钮。
  • 有一个“完成”按钮——当你在键盘上按下这个按钮时你想让键盘消失吗?
  • 我希望当我触摸屏幕上的任意位置(除了键盘)时,键盘会消失。
  • “当您按下视图中的任意位置时释放键盘(因为键盘没有退出按钮)。我确实将 IBActions 连接到 textFields 和按钮。”您正在连接他们的IBActions,例如触摸事件等。

标签: ios xcode


【解决方案1】:

您可以将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 方法。

【讨论】:

  • 这是正确的,但您也可以在笔尖编辑器中添加手势识别器。无需打字!只需将 Tap Gesture Recognizer 拖到视图中,然后选择手势识别器,转到连接检查器,然后从 Received Actions 拖到视图控制器并建立 IBAction 连接。
  • @MaurícioGiordano 我仍然会让键盘上的“完成”按钮也关闭键盘! :)
【解决方案2】:

只需在身份检查器中将视图的类从UIView 更改为UIControl,然后您就可以连接IBActions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多