我是iOS的新手,我正在学习本教程.
这是我尝试将IBAction连接到我的视图的屏幕截图.
我想在releaseKeyboard每次触摸视图时执行该方法(即关闭键盘).
我没有使用故事板.

我的文件:
challAppDelegate.h
#import <UIKit/UIKit.h>
@interface challAppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController;
@end
Run Code Online (Sandbox Code Playgroud)
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;
}
...
...
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
谢谢
您可以添加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];
}
Run Code Online (Sandbox Code Playgroud)
手势识别器的目标指向您的releaseKeyboardAction方法.
| 归档时间: |
|
| 查看次数: |
578 次 |
| 最近记录: |