获取 UITextField [textField frame] 时遇到问题;

发布于 2024-12-12 09:35:36 字数 1323 浏览 0 评论 0原文

当前代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"begin edit: %@", textField);
}

相关输出:

2011-10-30 09:12:08.436 My Project[83470:207] begin edit: <UITextField: 0x6c349d0; frame = (112 2; 182 39); text = 'My Name'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x6c34af0>>

所以我知道文本字段框架在那里,但是当我尝试抓住它时:

代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"begin edit: %@", [textField frame]);
    // also tried textField.frame -- same thing
}

错误:

Thread 1: EXC_BAD_ACCES (code=1, address=0x42e00000)

输出:

(lldb)

我一直在这方面旋转我的轮子,我不知道下一步该去哪里。感谢您阅读我的问题。

** 编辑 - 单元格 xib 实例化(文本字段所在的位置) **

注意: 文本字段来自只是一个表格单元格的 xib 文件。

static NSString *CellIdentifier = @"ValidatedTextViewTableCell";

ValidatedTextViewTableCell *cell = (ValidatedTextViewTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ValidatedTextViewTableCell" owner:self options:nil];
    cell = validatedTextViewTableCell;
    self.validatedTextViewTableCell = nil;
}

Current Code:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"begin edit: %@", textField);
}

Related output:

2011-10-30 09:12:08.436 My Project[83470:207] begin edit: <UITextField: 0x6c349d0; frame = (112 2; 182 39); text = 'My Name'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x6c34af0>>

So I know the textfield frame is there, but when I try to grab it:

code:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"begin edit: %@", [textField frame]);
    // also tried textField.frame -- same thing
}

error:

Thread 1: EXC_BAD_ACCES (code=1, address=0x42e00000)

output:

(lldb)

I've been spinning my wheels on this and I'm not sure where to go next. Thanks for reading my question.

** EDIT - cell xib instantiation (where the textfield lives) **

note: the textfield comes from a xib file that is just a table cell.

static NSString *CellIdentifier = @"ValidatedTextViewTableCell";

ValidatedTextViewTableCell *cell = (ValidatedTextViewTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ValidatedTextViewTableCell" owner:self options:nil];
    cell = validatedTextViewTableCell;
    self.validatedTextViewTableCell = nil;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-12-19 09:35:36

这是行不通的:

NSLog(@"begin edit: %@", [textField frame]);

因为 frame 是 CGRect 类型,而不是对象,因此仅适用于对象的 %@ 格式说明符在传递时会爆炸。您需要按每个组件记录帧,如下所示:

NSLog(@"begin edit: %f, %f, %fx%f", [textField frame].origin.x [textField frame].origin.y, [textField frame].size.width, [textField frame].size.height);

This won't work:

NSLog(@"begin edit: %@", [textField frame]);

Because frame is a CGRect type, not an object, so the %@ format specifier, which is for objects only, blows up when handed it. You'll need to log the frame by each of its components, like this:

NSLog(@"begin edit: %f, %f, %fx%f", [textField frame].origin.x [textField frame].origin.y, [textField frame].size.width, [textField frame].size.height);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文