分段控制中的 NSArray 问题
我使用此示例来创建分段视图。在我的 viewDidLoad 方法中,我收到警告并且代码崩溃。
- (void)viewDidLoad {
[super viewDidLoad];
self.segmentedViewControllers = [self segmentedViewControllerContent];
NSArray * segmentTitles = [self.segmentedViewControllers arrayByPerformingSelector:@selector(title)];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTitles];
self.segmentedControl.selectedSegmentIndex = 0;
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[self.segmentedControl addTarget:self
action:@selector(didChangeSegmentControl:)
forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = self.segmentedControl;
[self.segmentedControl release];
[self didChangeSegmentControl:self.segmentedControl];
}
我在这一行收到警告:
NSArray * segmentTitles = [self.segmentedViewControllers arrayByPerformingSelector:@selector(title)];
NSArray 可能不会响应 arrayByPerformingSelector。
I used this example to create a segmented view. In my viewDidLoad
method I am getting a warning and code crashes.
- (void)viewDidLoad {
[super viewDidLoad];
self.segmentedViewControllers = [self segmentedViewControllerContent];
NSArray * segmentTitles = [self.segmentedViewControllers arrayByPerformingSelector:@selector(title)];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTitles];
self.segmentedControl.selectedSegmentIndex = 0;
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[self.segmentedControl addTarget:self
action:@selector(didChangeSegmentControl:)
forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = self.segmentedControl;
[self.segmentedControl release];
[self didChangeSegmentControl:self.segmentedControl];
}
I am getting a warning in this line:
NSArray * segmentTitles = [self.segmentedViewControllers arrayByPerformingSelector:@selector(title)];
that NSArray may not respond to arrayByPerformingSelector.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
arrayByPerformingSelector:
不是发送到NSArray
的有效消息。该方法可能位于原始代码使用的NSArray
类别扩展之一中。检查您所关注的原始示例,并尝试查找arrayByPerformingSelector:
的定义位置,然后在代码中#import
该文件。arrayByPerformingSelector:
is not a valid message to be sent toNSArray
. This method is probably in one of the category extensions ofNSArray
that the original code uses. Check the original example you are following and try to find wherearrayByPerformingSelector:
is defined, then#import
that file in your code.arrayByPerformingSelector: 是有人编写的一种便利方法,以避免这样做:
但是,iOS 堆栈中已经有一些东西可以免费执行此操作!
您可以在NSHipsters 博客文章中阅读有关 KVC 收集操作的更多信息。对于一个不起眼的 API 来说,它实际上非常有用。我很遗憾我花了这么长时间才找到它。
arrayByPerformingSelector:
is a connivence method someone has written to avoid doing this:However, there is already something in the iOS stack that will do this for free!
You can read more about KVC collection operations in NSHipsters blog post. For an obscure API it's actually really useful. I am sad it took me this long to find it.