我已经完成了 UIBarButtonItem 到 UIToolBar 的动态添加,现在我正在尝试将其实现为方法 -(void)AddBarButton:withTitle (NSString*)title
并从事件处理程序(单击 UISearchBar 搜索结果行)。问题是我不知道如何实现将 UIBarButton 添加为单独的方法,最重要的是如何调用它(发送)?谁将是此消息的接收者? [self.toolBar addButton]
不工作。
对于这样的对于专业 iOS 开发人员来说可能是愚蠢的问题,我想道歉,因为我是 7 岁的 C++\Java 程序员多年,现在我不得不在没有任何经验的情况下在短时间内制作一个 iOS 项目。所以我真的需要帮助。
提前致谢!
最佳答案
我建议向您的方法添加更多参数以使其更加灵活(如果您不想或不需要这些参数,您可以从声明中删除它们并在方法实现中用固定值替换它们)。示例实现可能如下所示:
- (void)addBarButtonWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action animated:(BOOL)animated
{
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:title style:style target:target action:action];
NSMutableArray *items = [self.toolBar.items mutableCopy];
[items addObject:newButton];
[self.toolBar setItems:items animated:animated];
}
然后在您的事件处理程序中调用(根据需要修改参数):
[self addBarButtonWithTitle:@"someTitle" style:UIBarButtonItemStyleBordered target:self action:@selector(someMethod:) animated:YES];
关于objective-c - 如何定义和发送将 UIBarButtonItems 动态添加到 UIToolbar 的方法\消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13161267/