gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:45:01 25 4
gpt4 key购买 nike

我了解 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com