作为Objective-C中的外部代表 [英] Delegate as external class in Objective-C

查看:110
本文介绍了作为Objective-C中的外部代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPhone创建一个简单的项目,使用Xcode和Interface Builder。虽然我明白代理是什么,但我有使用它的问题。



我的界面中有一个UITextField。当用户点击它时,它显示键盘,但是我需要手动编程如何隐藏键盘。可以使用代理完成。所以在IB中,我从图书馆取对象,把它的类名称作为Control1Delegate,然后将代理插座从我的文本框连接成这个Control1Delegate。我也有这个Control1Delegate类的.m和.h文件:



Control1Delegate.h

  @interface Control1Delegate:NSObject< UITextFieldDelegate> {
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;

@end

Control1Delegate.m

  #importControl1Delegate.h

@implementation Control1Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
返回YES;
}

@end

但这不行。运行时,它永远不会达到textFieldShouldReturn方法或崩溃,没有msg或EXEC_BAD_ACCESS。有趣的是,当我将方法移动到控制器文件(一个向导生成)中并从UITextField连接到该控制器(文件所有者)时,一切都按预期方式运行。我看到大多数苹果代码教程将委托方法放入随机对象中,而不是单独的类 - 我想知道为什么。我不能在单独的班级中有代表?



我在这里缺少什么?一些空指针? Object livecycle?

解决方案

您的Control1Delegate对象在创建后不久就会被破坏。如果要保持活动,必须保留所有顶级的Nib对象。请参阅资源编程指南:Nib对象生命周期



文件所有者可以拥有这样的属性,以保留对象:

  @property(nonatomic,retain)IBOutlet Control1Delegate * control1delegate; 

记住在不再需要对象后释放对象。


I'm creating a simple project for iPhone, using Xcode and Interface Builder. While I understand what a delegate is, I have a problem with using it.

I have an UITextField in my interface. It displays keyboard when user taps on it, but I need to program manually how to hide keyboard. It can be done using delegates. So in IB, I'm taking Object from library, giving it's class name as Control1Delegate and then connecting the delegate outlet from my textfield to be this Control1Delegate. I also have .m and .h files for this Control1Delegate class :

Control1Delegate.h

@interface Control1Delegate : NSObject <UITextFieldDelegate> {
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField;

@end

Control1Delegate.m

#import "Control1Delegate.h"

@implementation Control1Delegate

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

@end

But this doesn't work. When running, it just never reaches the textFieldShouldReturn method or crashes without msg or with EXEC_BAD_ACCESS. The funny thing is that when I move the method into controller file (the one wizard has generated) and connect from UITextField to this controller (File's Owner), everything works as expected. I saw that most apple code tutorial puts delegated methods into random objects rather then separate class - I would like to know why. Can't I have delegate in separate class?

What I'm missing here? Some null pointer? Object livecycle?

解决方案

Your Control1Delegate object is getting destroyed soon after it is created. All top-level Nib objects must be retained if you want to keep them alive. Refer to the Resource Programming Guide: The Nib Object Life Cycle.

The File's Owner could have a property like this, in order to retain the object:

@property (nonatomic, retain) IBOutlet Control1Delegate *control1delegate;

Remember to release the object after it is no longer needed.

这篇关于作为Objective-C中的外部代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆