iphone - 对于具有 UITextField 的自定义警报 View ,发布问题有点复杂

标签 iphone ios release

我了解 iOS 5 中的 ARC,但我现在正在开发 iOS 5 之前的代码风格,并且希望通过手动发布的方式解决这个问题。

我的唯一目标是使用 UITextField 创建一个非常方便的自定义警报 View 。

我有一个“BigView” View ,其中有很多功能。它可能会为该 View 的显示器上的许多不同情况生成许多 UIAlertView。所以我知道为每个警报 View 使用 UIAlertViewDelegate 的方法,但有点实验性地尝试让它像 UIButton 的“addTarget”一样(实际上是 UIControl 的方法)。

简单地说,

这是“BigView”类和我的“TextAlert”实例的一部分,由电子邮件收集按钮触发。

BigView.m

- (void)emailFeedback:(id)sender
{
    TextAlert *textAlert = [[TextAlert alloc] initWithTitle:@"Enter your email address"];
    [textAlert setTarget:self action:@selector(textAlertInputed:)];
//    [textAlert release];
}

- (void)textAlertInputed:(NSString *)text
{
    NSLog(@"text alert inputed, text: %@", text);
}

这些都是我的 TextAlert 文件。

TextAlert.h

#import <Foundation/Foundation.h>

@interface TextAlert : NSObject <UIAlertViewDelegate>
{
    UIAlertView *alertView;
    UITextField *textField;
    id target;
    SEL action;
}

- (id)initWithTitle:(NSString *)title;
- (void)setTarget:(id)target action:(SEL)action;

@end

TextAlert.m

#import "TextAlert.h"

@implementation TextAlert

- (id)initWithTitle:(NSString *)title
{
    if (self = [super init]) 
    {
        alertView = [[UIAlertView alloc] initWithTitle:title message:@"beneath" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
        CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
        [alertView setTransform:myTransform];
        [textField setBackgroundColor:[UIColor whiteColor]];
        [alertView addSubview:textField];
        [alertView show];

    }
    return self;
}

- (void)dealloc
{
    [alertView release]; alertView = nil;
    [textField release]; textField = nil;
    [super dealloc];
}

- (void)setTarget:(id)_target action:(SEL)_action
{
    target = _target;
    action = _action;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [target performSelector:action withObject:textField.text];
}

@end

所以我的主要问题是“BigView”中 TextAlert 实例的释放点,因为您可以看到上面唯一的注释部分完整代码。当然,如果我删除该注释,我会因调用释放方法而崩溃。

我还收到错误 make textAlert 实例作为自动释放的实例。

对我来说,唯一的解决方案是使“BigView”中的“textAlert”对象成为“BigView”的成员而不是本地对象。但在这种情况下,我认为,我最初的方便和轻量级方法的目标并没有得到满足。而且“BigView”已经有很多成员实例,所以我不想再添加了。

那么有什么建议吗?或者欢迎对此尝试提出任何评论。我已经准备好听任何 真的责备我的代码不足。

提前致谢,

MK

最佳答案

如果除了发布问题之外一切正常,您应该只考虑实现公共(public)“show”方法和私有(private)“dismiss”方法(在您的自定义警报 View 中)。在显示方法中,您应该在其他事物旁边调用 [self keep]驳回(将此目标添加到按钮或任何驳回您的 View 的内容)调用 [self relese]。

关于iphone - 对于具有 UITextField 的自定义警报 View ,发布问题有点复杂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784535/

相关文章:

iphone - 显示 UIAlertView 会导致 EXC_BAD_ACCESS

iphone - 使用单个 NSMutableArray 填充 UITableView 部分表

ios - 如何以编程方式获取 "version code"和 "model code"

ios - CLLocationManager 未确定位置

asp.net - 编译调试 ="false"和 Release模式有什么区别?

iphone - 如何在 iPhone 上将声音名称传递给 Fmod

ios - 有没有办法更改 UILocalNotification 的时间之前的文本?

ios - 从后台返回后崩溃

android - 谷歌通常需要多长时间来审核封闭测试中的应用程序?

Cocoa NSWindowController 和 NSWindow 未解除分配