objective-c - 无法识别的选择器发送到实例 - 尝试检查 View 中的 uilabel 文本值

标签 objective-c ios

首先,我想说的是,我知道下面要展示的代码可能效率很低,但我正在“艰难地”这样做,因为这通常是我在学习时让事情坚持下来的方式...

我创建了一个在运行时调用的类,其中有一个标签(我将动态添加更多标签等)。当我检查将保存这些 View 的容器时,我首先检查看看其中是否还有其他 View 。如果没有,我直接添加 View 。如果那里已经有其他 View ,我需要检查它是否不会将两个相同的项目放在那里。由于这些都是基于单个类,所以我当前的疯狂路线是使用我在 xib 中为此类设置的“headerLabel”导出。所以...

在PhysicalExamDetailsNoteItem.h中:

#import <UIKit/UIKit.h>

@interface PhysicalExamDetailsNoteItem : UIViewController {
    IBOutlet UILabel *headerLabel;
}

@property(nonatomic, retain)UILabel *headerLabel;

@end

在PhysicalExamDetailsNoteItem.m中,我添加:@synthesize headerLabel;在@implementation之后。

在 ViewController.m 中:

#import "PhysicalExamDetailsNoteItem.h"

// ... And after a long list of code-based view setup, I call a method that includes the following ... //

            if([[physExamDetailsNoteItems subviews] count] > 0)
        {
            NSLog(@"More than one item already exists in the container, check preexisting items for %@ to eliminate duplication...", sender.currentTitle);
            for (PhysicalExamDetailsNoteItem *subview in [physExamDetailsNoteItems subviews]) {
                NSLog(@"subview.headerLabel.text = %@", subview.headerLabel.text);
                if (subview.headerLabel.text != sender.currentTitle) {
                    NSLog(@"%@ doesn't exist in the container, add it as a new item...", sender.currentTitle);
                    PhysicalExamDetailsNoteItem *noteItem;
                    noteItem = [[PhysicalExamDetailsNoteItem alloc] initWithNibName:@"PhysicalExamDetailsNoteItem" bundle:[NSBundle mainBundle]];
                    [physExamDetailsNoteItems addSubview:noteItem.view];
                    noteItem.headerLabel.text = sender.currentTitle;
                }
            }
        }
        else
        {
            NSLog(@"No items exist in the container, add a new item...");
            PhysicalExamDetailsNoteItem *noteItem;
            noteItem = [[PhysicalExamDetailsNoteItem alloc] initWithNibName:@"PhysicalExamDetailsNoteItem" bundle:[NSBundle mainBundle]];
            [physExamDetailsNoteItems addSubview:noteItem.view];
            noteItem.headerLabel.text = sender.currentTitle;                
        }

我在此部分收到错误:

PhysicalExamDetailsNoteItem *subview in [physExamDetailsNoteItems subviews]) {
                NSLog(@"subview.headerLabel.text = %@", subview.headerLabel.text);

我将 headerLabel 导出从文件所有者对象连接到 IB 中的标签 View ,当封闭方法第一次运行 if 条件时,它使用 else 子句并填充我的容器 View 并更改标签文本正如我想要的那样,但是一旦再次调用它并查找 headerLabel 进行比较,它就会失败,我得到:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView headerLabel]:无法识别的选择器发送到实例 0x4b17470”

提前致谢!

最佳答案

因为您要求所有 subview ,所以您确实获得了所有 subview 。显然,其中一个或多个不属于您正在寻找的类别。指望给定 View 的 subview 仅是您自己添加的 View 通常是不安全的。

您绝对应该按照 ennukiller 的建议更改快速枚举声明以使用 UIView 类。但是解决崩溃的方法是在调用方法之前测试给定的 subview 是否属于预期的类,如下所示:

for (UIView * subview in [physExamDetailsNoteItems subviews]) {
    if ([subview isKindOfClass:[PhysicalExamDetailsNoteItem class]]) {
        PhysicalExamDetailsNoteItem * noteItem = (PhysicalExamDetailsNoteItem *)subview;
        NSLog(@"noteItem.headerLabel.text = %@", noteItem.headerLabel.text);
    }
}

这样,您将使用动态运行时来确保您正在操作的对象是您期望的类型。

关于objective-c - 无法识别的选择器发送到实例 - 尝试检查 View 中的 uilabel 文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4400585/

相关文章:

ios - 将 nil 作为参数传递给 stringWithString 时出现异常 :?

objective-c - 我是否正确使用Core Location?它似乎不起作用

ios - 如何在 iOS 版 swift 中使用 vDSP/Accelerate 计算向量元素的平方根

ios - Major Bug,在XCAssets 文件夹中创建的Sprite Atlas 不支持批量渲染

ios - UITextField iOS : how to deal with the dismiss keyboard button?

ios - 将 subview 添加到 ScrollView

objective-c - NSKeyedArchiver .plist 附加问题

ios - 观看应用程序自定义菜单图像未显示?

objective-c - iOS 6,转场 : prepareForSegue doesn't get called when tapping on UITableViewCell at first time

iphone - 从主屏幕重新打开应用程序时如何刷新标签栏内容? (iOS/Xcode)