objective-c - iOS 键盘返回处理程序

标签 objective-c ios

我是 Mac、iOS、Xcode 和 objective-c 的新手。我非常熟悉 Windows 世界(.net、C#、Silverlight 等),但这些都不适用于我在 Mac 上所做的事情。

因此,简而言之,我希望您能通过各种指导和代码审查来帮助我。网络上有很多文章(其中很多在 stack overflow 上),您很容易迷失在关于如何做事的所有观点中。

所以我决定尝试一些简单的事情,比如让 iOS 键盘对下一步和完成使用react,在键盘上方显示一个工具栏,并移动文本字段以便为键盘留出空间。我这里的第一个示例是简单的示例,三个文本字段,两个带有下一步,最后一个带有关闭键盘的完成。

enter image description here

我在头文件中为每个文本字段添加了 UITextFieldDelegate 和一个 IBOutlet。同时按住 Ctrl 键将文本字段拖到 IBOutlet 上以连接它。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField* firstNameTextField;
@property (weak, nonatomic) IBOutlet UITextField* lastNameTextField;
@property (weak, nonatomic) IBOutlet UITextField* cityTextField;

@end

在源文件中,我为文本字段添加了综合语句,在 viewDidUnload 中设置为 nil 以标记要释放的内存(使用自动内存 Release模式)和 textFieldShouldReturn 方法将焦点设置在文本字段上.

#import "ViewController.h"

@implementation ViewController

@synthesize firstNameTextField = _firstNameTextField;
@synthesize lastNameTextField = _lastNameTextField;
@synthesize cityTextField = _cityTextField;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

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

- (void)viewDidUnload
{
    [self setFirstNameTextField:nil];
    [self setLastNameTextField:nil];
    [self setCityTextField:nil];

    [super viewDidUnload];    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    if (theTextField == self.firstNameTextField)
    {
        [self.lastNameTextField becomeFirstResponder];
    }
    else if (theTextField == self.lastNameTextField)
    {
        [self.cityTextField becomeFirstResponder];
    }
    else
    {
        [theTextField resignFirstResponder];
    }

    return YES;
}
@end

你会这样做吗?

从 objective-c 的角度来看,您会将 *(表示指针)与类型放在一起还是放在名称前面?

@property (weak, nonatomic) IBOutlet UITextField* firstNameTextField;

对比

@property (weak, nonatomic) IBOutlet UITextField *firstNameTextField;

内存管理是否正确?内存会被正确释放,保持存活等吗?

谢谢。

最佳答案

正如安东尼所说,星号的位置确实是一个风格问题。我个人更喜欢实例 (Class *classInstance) 旁边的 *,因为它是您指向的实例,而不是类。但意见不一。

总的来说,代码没有问题,除了您将所有属性都定义为弱。因为您想在 View Controller 的整个生命周期内保留这些 IBOutlet 属性,所以它们应该很强大。即使您已经在 IB 中构建了 View ,您的 View Controller 仍然“拥有”这些 IB 对象。

当您将属性类型定义为:

@property (strong, nonatomic) AClass *aClassInstance;

这意味着您的对象在分配时将被保留。在这种情况下,分配是通过连接你的 socket 而发生的,你没有手动分配,但保留仍然发生:

[self setProperty:object] //retains 'object'

当你将它置为零时释放:

[self setProperty:nil] //releases 'object' currently retained by property

如果/当(正如您所做的那样)您确实想要一个弱属性(例如,引用一个您不拥有的对象):

@property (weak, nonatomic)

以后您不需要通过在 viewDidUnload 中将其设置为 nil 来“释放”它。将它设置为 nil 仍然是一个好习惯,但不需要平衡你的内存。

关于objective-c - iOS 键盘返回处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8919597/

相关文章:

ios - 自定义 Action 控件 Swift 3.0

iphone - 如何在 iPhone 中使用 SQLite?

ios - Swift 2.3 中没有这样的模块 "UserNotifications"

objective-c - 自定义键盘应用程序扩展 iOS 8 上的 Siri

objective-c - 自定义 UINavigationBar 使其不可见,但保持按钮可见

objective-c - Objective C - init 和构造函数之间的区别?

ios - 如何根据日期对 tableView 进行排序?

ios - 更改 UIAlertView 中按钮的辅助功能标签 - iOS

objective-c - 关于 "Semantic Issue"的编译器警告——如何解决?

ios - 如何使用 NSUserDefaults 使用 NSMutableArray 中的数据保存数据