2011-08-10 44 views
0

快要疯了用的UITextField输入我有一个用下面的代码引发了PromoCodeViewController:在一个非常简单的应用程序

@implementation Demo_WebServiceCallingUsingiOSAppDelegate 

@synthesize window = _window,promoCodeController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    self.promoCodeController = [[PromoCodeViewController alloc] init]; 

    [self.window addSubview:self.promoCodeController.view]; 

    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

的PromoCodeViewController包含的UITextField和我落实UITextFieldDelegate为PromoCodeViewController如图所示:

@interface PromoCodeViewController : UIViewController<UITextFieldDelegate> 

{ 
    IBOutlet UITextField *promoCodeTextField; 
} 

@property (nonatomic,retain) IBOutlet UITextField *promoCodeTextField; 

@end 

我实现textFieldShouldReturn方法如下所示:

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    return TRUE; 
} 

我甚至已经将PromoCodeViewController设置为UITextField事件的委托。当我开始在TextBox中输入时,它会抛出“Program received signal。EXC_BAD_ACCESS”。当我在UITextField中输入第二个字符时会发生这种情况。我究竟做错了什么?

更新1:

的错误出现在以下部分:

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([Demo_WebServiceCallingUsingiOSAppDelegate class])); 
    } 
} 
+0

,我不认为你应该你的窗口在第一个代码块被设置为你promoCodeViewController ......虽然我不认为这是什么原因造成的错误。 –

+0

什么是崩溃时的堆栈跟踪? – sergio

+0

@sergio栈跟踪没有任何东西! – azamsharp

回答

0

看起来好像你错过了promoCodeTextField财产@synthesize声明。以下内容添加到您的Demo_WebServiceCallingUsingiOSAppDelegate类(顺便说一下,可能来自重命名受益):

@synthesize promoCodeTextField = _promoCodeTextField; 

没有@synthesize声明,Xcode的笔尖文件编辑器将直接设置,而不是调用访问方法的实例变量(因为它们不存在);因此文本字段永远不会被保留。

+0

我有综合!这是模拟器中的BUG! – azamsharp

+0

在您发布的代码中没有'promoCodeTextField'的'@ synthesize'语句。 – jlehr

+0

确实'promoCodeTextField'不合成。但这不是问题。首先,outlet是直接在实例变量上声明的,所以nib加载机制将使用'setValue:forKey:',如果没有setter可以直接访问变量。在这种情况下,[documentation](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html)指出该值将被保留。即使情况并非如此,包含视图仍会保留文本字段。 – omz

0

什么对象是文本框的代表?对象通常不会保留它们的委托(因为委托通常保留对该对象的引用,并且保留循环导致内存泄漏)。

我的直觉是你的文本字段的委托定义和连接在你的笔尖,但没有被保留的任何地方,笔尖加载的委托被释放后不久如此。只要文本字段尝试对代理执行任何操作,它就会崩溃。

如果是这样,请务必东西(通常是应用程序委托)被保持到任何对象作为你的文本字段的委托提供参考。

相关问题